wsgiservlets.Dispatcher

class wsgiservlets.Dispatcher(docroot=None, servlet_mapping={}, **user_config)

Dispatcher does not generate any content, but is a dispatching agent in the truest sense: incoming requests are processed and passed on to another agent which handles the request. Dispatchers determine how to pass on the request by examining PATH_INFO and provide a means to intermix several servlets along with static file content under the control of one servlet.

__init__(docroot=None, servlet_mapping={}, **user_config)
Parameters:
  • docroot (str or None) – the path to the document root containing static content. This must be an existing directory. If None (the default), static content serving will be disabled.
  • servlet_mapping (dict) –

    a mapping of names to servlet classes of the form:

    name : WSGIServletClass

    or:

    name : (WSGIServletClass, args [, kw])

    In the first form, the name maps to a WSGIServlet class (not an instance). In the second, a tuple is specified with args, a tuple, and kw, a dict, the positional parameters and keyword arguments, respectively, to be passed to the constructor of the servlet class. If not specified, args defaults to an empty tuple and kw an empty dict.

  • user_config – extra keyword arguments will be merged with default_config to modify behaviour of the dispatcher.

After a dispatcher receives a request, PATH_INFO is inspected and the first component (i.e., path_info_list[0]), if it exists, is looked up in servlet_mapping and if a servlet is found an instance is created:

servlet = WSGIServletClass(*args, **kw)

WSGIServlet.pop_path_info() is called to shift the servlet name to SCRIPT_NAME, then WSGIServlet.subrequest() is called on the servlet instance, processing the request and returning the results to the client.

If a servlet class is not found in servlet_mapping, it is assumed PATH_INFO refers to a static file relative to docroot. If such a file exists, the file is returned as the content.

If no mapping is found and no file is found (or docroot was never specified), HTTPNotFound is raised.

Note

servlets handling requests on behalf of a Dispatcher will have in their environment a key, "wsgiservlets.dispatcher", with value the Dispatcher instance.

default_config

A mapping that holds variables which control dispatching behaviour:

  • default_servlet (str) – if PATH_INFO is empty, attempt a redirect to this servlet. (default: "index")
  • dirindex (seq) – a sequence of file names to be searched (in order of the seq) when PATH_INFO references a directory. (default: ("index.html", "index.htm"))
  • forbid_startswith (seq) – a sequence of filename prefixes which are forbidden in any one component of the path. (default: (".", "_"))
  • forbid_extensions (seq) – a sequence of file extensions which are forbidden. (default: (".py", ".pyc", ".pyo", ".wsgi"))

Previous topic

wsgiservlets.HTMLPage

Next topic

wsgiservlets.JSONRPCServer

This Page