libbe.util.wsgi

Utilities for building WSGI commands.

See Also

libbe.command.serve_storage and libbe.command.serve_commands.

class libbe.util.wsgi.AdminApp(users=None, setting='be-auth', *args, **kwargs)

WSGI middleware for managing users

Changing passwords, usernames, etc.

Methods

admin(environ, start_response)
data_get_boolean(data, key[, default, source])
data_get_id(data[, key, default, source])
data_get_string(data, key[, default, source])
error(environ, start_response, error, message) Make it easy to call start_response for errors.
is_head(environ)
log_request(environ[, status, bytes])
ok_response(environ, start_response, content)
post_data(environ)
query_data(environ)
admin(environ, start_response)
class libbe.util.wsgi.AuthenticationApp(realm, setting='be-auth', users=None, *args, **kwargs)

WSGI middleware for handling user authentication.

Methods

authenticate(environ) Handle user-authentication sent in the “Authorization” header.
authfunc(environ, username, password)
error(environ, start_response, error, message) Make it easy to call start_response for errors.
log_request(environ[, status, bytes])
authenticate(environ)

Handle user-authentication sent in the “Authorization” header.

This function implements Basic authentication as described in HTTP/1.0 specification [R3] . Do not use this module unless you are using SSL, as it transmits unencrypted passwords.

[R3]http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html#BasicAA

Notes

Code based on authkit/authenticate/basic.py (c) 2005 Clark C. Evans. Released under the MIT License: http://www.opensource.org/licenses/mit-license.php

Examples

>>> users = Users()
>>> users.add_user(User('Aladdin', 'Big Al', password='open sesame'))
>>> app = AuthenticationApp(app=None, realm='Dummy Realm', users=users)
>>> app.authenticate({'HTTP_AUTHORIZATION':'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='})
'Aladdin'
>>> app.authenticate({'HTTP_AUTHORIZATION':'Basic AAAAAAAAAAAAAAAAAAAAAAAAAA=='})
authfunc(environ, username, password)
class libbe.util.wsgi.BEExceptionApp(*args, **kwargs)

Translate BE-specific exceptions

Methods

error(environ, start_response, error, message) Make it easy to call start_response for errors.
log_request(environ[, status, bytes])
class libbe.util.wsgi.ExceptionApp(app, *args, **kwargs)

Some servers (e.g. cherrypy) eat app-raised exceptions.

Work around that by logging tracebacks by hand.

Methods

error(environ, start_response, error, message) Make it easy to call start_response for errors.
log_request(environ[, status, bytes])
exception libbe.util.wsgi.HandlerError(code, msg, headers=[])
class libbe.util.wsgi.HandlerErrorApp(app, *args, **kwargs)

Catch HandlerErrors and return HTTP error pages.

Methods

error(environ, start_response, error, message) Make it easy to call start_response for errors.
log_request(environ[, status, bytes])
class libbe.util.wsgi.ServerCommand(*args, **kwargs)

Serve something over HTTP.

Use this as a base class to build commands that serve a web interface.

Methods

cleanup()
complete([argument, fragment])
help(*args)
run([options, args])
usage()
class libbe.util.wsgi.SilentRequestHandler(request, client_address, server)

Attributes

timeout

Methods

MessageClass
address_string() Return the client address formatted for logging.
date_time_string([timestamp]) Return the current date and time formatted for a message header.
end_headers() Send the blank line ending the MIME headers.
finish()
get_environ()
get_stderr()
handle() Handle a single HTTP request
handle_one_request() Handle a single HTTP request.
log_date_time_string() Return the current time formatted for logging.
log_error(format, *args) Log an error.
log_message(format, *args)
log_request([code, size]) Log an accepted request.
parse_request() Parse a request (internal).
send_error(code[, message]) Send and log an error reply.
send_header(keyword, value) Send a MIME header.
send_response(code[, message]) Send the response header and log the response code.
setup()
version_string() Return the server software version string.
log_message(format, *args)
exception libbe.util.wsgi.Unauthenticated(realm, msg='User Not Authenticated', headers=[])
exception libbe.util.wsgi.Unauthorized(msg='User Not Authorized', headers=[])
class libbe.util.wsgi.UppercaseHeaderApp(app, *args, **kwargs)

WSGI middleware that uppercases incoming HTTP headers.

From PEP 333, The start_response() Callable :

A reminder for server/gateway authors: HTTP header names are case-insensitive, so be sure to take that into consideration when examining application-supplied headers!

Methods

error(environ, start_response, error, message) Make it easy to call start_response for errors.
log_request(environ[, status, bytes])
class libbe.util.wsgi.User(uname=None, name=None, passhash=None, password=None)

Methods

from_string(string)
hash(password)
set_name(name)
set_password(password)
valid_login(password)
from_string(string)
hash(password)
set_name(name)
set_password(password)
valid_login(password)
class libbe.util.wsgi.Users(filename=None)

Methods

add_user(user)
clear(() -> None.  Remove all items from D.)
copy(() -> a shallow copy of D)
fromkeys(...) v defaults to None.
get((k[,d]) -> D[k] if k in D, ...)
has_key((k) -> True if D has a key k, else False)
items(() -> list of D’s (key, value) pairs, ...)
iteritems(() -> an iterator over the (key, ...)
iterkeys(() -> an iterator over the keys of D)
itervalues(...)
keys(() -> list of D’s keys)
load()
pop((k[,d]) -> v, ...) If key is not found, d is returned if given, otherwise KeyError is raised
popitem(() -> (k, v), ...) 2-tuple; but raise KeyError if D is empty.
save()
setdefault((k[,d]) -> D.get(k,d), ...)
update(([E, ...) If E present and has a .keys() method, does: for k in E: D[k] = E[k]
valid_login(uname, password)
values(() -> list of D’s values)
viewitems(...)
viewkeys(...)
viewvalues(...)
add_user(user)
load()
save()
valid_login(uname, password)
class libbe.util.wsgi.WSGICaller(*args, **kwargs)

Call into WSGI apps programmatically

Methods

getURL(app[, path, method, data, data_dict, ...])
start_response(status, response_headers[, ...])
getURL(app, path='/', method='GET', data=None, data_dict=None, scheme='http', environ={})
start_response(status, response_headers, exc_info=None)
class libbe.util.wsgi.WSGI_AppObject(urls=(), default_handler=None, setting='be-server', *args, **kwargs)

Useful WSGI utilities for handling URL delegation.

Methods

error(environ, start_response, error, message) Make it easy to call start_response for errors.
log_request(environ[, status, bytes])
class libbe.util.wsgi.WSGI_DataObject(*args, **kwargs)

Useful WSGI utilities for handling data (POST, QUERY) and returning responses.

Methods

data_get_boolean(data, key[, default, source])
data_get_id(data[, key, default, source])
data_get_string(data, key[, default, source])
error(environ, start_response, error, message) Make it easy to call start_response for errors.
is_head(environ)
log_request(environ[, status, bytes])
ok_response(environ, start_response, content)
post_data(environ)
query_data(environ)
data_get_boolean(data, key, default=False, source='query')
data_get_id(data, key='id', default=<class 'libbe.util.wsgi.HandlerError'>, source='query')
data_get_string(data, key, default=None, source='query')
is_head(environ)
ok_response(environ, start_response, content, content_type='application/octet-stream', headers=[])
post_data(environ)
query_data(environ)
class libbe.util.wsgi.WSGI_Middleware(app, *args, **kwargs)

Utility class for WGSI middleware.

Methods

error(environ, start_response, error, message) Make it easy to call start_response for errors.
log_request(environ[, status, bytes])
class libbe.util.wsgi.WSGI_Object(logger=None, log_level=20, log_format=None)

Utility class for WGSI clients and middleware.

For details on WGSI, see PEP 333

Methods

error(environ, start_response, error, message) Make it easy to call start_response for errors.
log_request(environ[, status, bytes])
error(environ, start_response, error, message, headers=[])

Make it easy to call start_response for errors.

log_request(environ, status='-1 OK', bytes=-1)

Table Of Contents

This Page