webtools.wsgi

class webtools.wsgi.Config(defaults=None)

A simple configuration dictionary for the WSGIApplication.

loaded = None

Loaded configurations.

load_config(key, default_values=None, user_values=None, required_keys=None)

Returns a configuration for a given key.

This can be used by objects that define a default configuration. It will update the app configuration with the default values the first time it is requested, and mark the key as loaded.

Parameters:
  • key – A configuration key.
  • default_values – Default values defined by a module or class.
  • user_values – User values, used when an object can be initialized with configuration. This overrides the app configuration.
  • required_keys – Keys that can not be None.
Raises:

Exception, when a required key is not set or is None.

class webtools.wsgi.WSGIApplication(routes=None, debug=False, config=None)

A WSGI-compliant application.

allowed_methods = frozenset(['HEAD', 'TRACE', 'GET', 'PUT', 'POST', 'OPTIONS', 'DELETE'])

Allowed request methods.

request_class

Class used for the request object.

alias of Request

response_class

Class used for the response object.

alias of Response

router_class

Class used for the router object.

alias of Router

request_context_class

Class used for the request context object.

alias of RequestContext

config_class

Class used for the configuration object.

alias of Config

app = <LocalProxy unbound>

Active WSGIApplication instance. See set_globals().

request = <LocalProxy unbound>

Active Request instance. See set_globals().

active_instance = <LocalProxy unbound>

Same as app, for webapp compatibility. See set_globals().

debug = False

A general purpose flag to indicate development mode: if True, uncaught exceptions are raised instead of using HTTPInternalServerError.

registry = None

A dictionary to register objects used during the app lifetime.

error_handlers = None

A dictionary mapping HTTP error codes to callables to handle those HTTP exceptions. See handle_exception().

config = None

A Config instance with the application configuration.

router = None

A Router instance with all URIs registered for the application.

set_globals(app=None, request=None)

Registers the global variables for app and request.

If webbox.utils.local is available the app and request class attributes are assigned to a proxy object that returns them using thread-local, making the application thread-safe. This can also be used in environments that don’t support threading.

If webbox.utils.local is not available app and request will be assigned directly as class attributes. This should only be used in non-threaded environments.

Parameters:
clear_globals()

Clears global variables. See set_globals().

handle_exception(request, response, e)

Handles a uncaught exception occurred in __call__().

Uncaught exceptions can be handled by error handlers registered in error_handlers. This is a dictionary that maps HTTP status codes to callables that will handle the corresponding error code. If the exception is not an HTTPException, the status code 500 is used.

The error handlers receive (request, response, exception) and can be a callable or a string in dotted notation to be lazily imported.

If no error handler is found, the exception is re-raised.

Based on idea from Flask.

Parameters:
  • request – A Request instance.
  • request – A Response instance.
  • e – The uncaught exception.
Returns:

The returned value from the error handler.

run()

Runs this WSGI-compliant application in a CGI environment. Uses wsgiref.handlers.CGIHandler().run().

get_response(*args, **kwargs)

Creates a request and returns a response for this app.

This is a convenience for unit testing purposes. It receives parameters to build a request and calls the application, returning the resulting response:

class HelloHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello, world!')

app = webapp2.WSGIapplication([('/', HelloHandler)])

# Test the app, passing parameters to build a request.
response = app.get_response('/')
assert response.status_int == 200
assert response.body == 'Hello, world!'
Parameters:
  • args – Positional arguments to be passed to Request.blank().
  • kwargs – Keyword arguments to be passed to Request.blank().
Returns:

A Response object.

Previous topic

webtools

Next topic

webtools.route

This Page