webtools.handlers

class webtools.handlers.RequestHandler(request=None, response=None)

Base HTTP request handler.

request = None

A webtools.request.Request instance.

response = None

A webtools.response.Response instance.

app = None

A webtools.wsgi.WSGIApplication instance.

authorize = None

Validates if a request has permission to view the requested resource. Authorize should be set to a callable that accepts one parameter, a webtools.handlers.RequestHandler subclass instance. The callable should return True if the request is allowed to proceed and False otherwise. If False, a 401 Not Authorized error will be raised:

def my_authorizer(handler):
    # Maybe do something with the request object attached to the handler
    if request_is_authorized:
        return True
    else:
        return False

class MyHandler(RequestHandler):
    authorize = my_authorizer

    def get(self):
        self.response.write('Only authorized requests can see this')
authorize_methods = []

Lists the HTTP methods that authorize() should validate. By default, authorize() validates all methods. If set, authorize() will validate only those HTTP methods specified and ignore any others:

class MyHandler(RequestHandler):
    authorize = my_authorizer
    authorize_methods = ['post']

    def get(self):
        self.response.write('Anyone can see a GET request')

    def post(self):
        self.response.write('Only those authorized can POST')
cache_control = ''

Sets the HTTP Cache-Control header in the response for GET requests. Defaults to the value specified by default_cache_control if blank. Care should be taken when setting this attribute.

default_cache_control = 'max-age=0, no-cache, no-store'

Sets the default value for the HTTP Cache-Control header in the response. This applies to all HTTP request methods (i.e. GET, POST, etc..). This attibute should not be modified in most use casses.

initialize(request, response)

Initializes this request handler with the given WSGI application, Request and Response.

Parameters:
  • request – A Request instance.
  • response – A Response instance.
dispatch()

Dispatches the request.

This will first check if there’s a handler_method defined in the matched route, and if not it’ll use the method correspondent to the request method (get(), post() etc).

error(code)

Clears the response and sets the given HTTP status code.

This doesn’t stop code execution; for this, use abort().

Parameters:code – HTTP status error code (e.g., 501).
abort(code, *args, **kwargs)

Raises an HTTPException.

This stops code execution, leaving the HTTP exception to be handled by an exception handler.

Parameters:
  • code – HTTP status code (e.g., 404).
  • args – Positional arguments to be passed to the exception class.
  • kwargs – Keyword arguments to be passed to the exception class.
redirect(uri, permanent=False, abort=False, code=None, body=None)

Issues an HTTP redirect to the given relative URI.

The arguments are described in redirect().

redirect_to(_name, _permanent=False, _abort=False, _code=None, _body=None, *args, **kwargs)

Convenience method mixing redirect() and uri_for().

The arguments are described in redirect() and uri_for().

uri_for(_name, *args, **kwargs)

Returns a URI for a named Route.

handle_exception(exception, debug)

Called if this handler throws an exception during execution.

The default behavior is to re-raise the exception to be handled by WSGIApplication.handle_exception().

Parameters:
  • exception – The exception that was thrown.
  • debug_mode – True if the web application is running in debug mode.
class webtools.handlers.RedirectHandler(request=None, response=None)

Redirects to the given URI for all GET requests.

This is intended to be used when defining URI routes. You must provide at least the keyword argument url in the route default values. Example:

def get_redirect_url(handler, *args, **kwargs):
    return handler.uri_for('new-route-name')

app = WSGIApplication([
    Route('/old-url', RedirectHandler, defaults={'_uri': '/new-url'}),
    Route('/other-old-url', RedirectHandler, defaults={'_uri': get_redirect_url}),
])
get(*args, **kwargs)

Performs a redirect.

Two keyword arguments can be passed through the URI route:

  • _uri: A URI string or a callable that returns a URI. The callable is called passing (handler, *args, **kwargs) as arguments.
  • _code: The redirect status code. Default is 301 (permanent redirect).

Previous topic

webtools.route

Next topic

webtools.helpers

This Page