paste.urlparser – Handle URL paths and server static files
WSGI applications that parse the URL and dispatch to on-disk resources
class paste.urlparser.StaticURLParser(directory, root_directory=None, cache_max_age=None)
Like URLParser but only serves static files.
- integer specifies Cache-Control max_age in seconds
paste.urlparser.make_static(global_conf, document_root, cache_max_age=None)
Return a WSGI application that serves a directory (configured
cache_max_age - integer specifies CACHE_CONTROL max_age in seconds
class paste.urlparser.PkgResourcesParser(egg_or_spec, resource_name, manager=None, root_resource=None)
paste.urlparser.make_pkg_resources(global_conf, egg, resource_name='')
A static file parser that loads data from an egg using
pkg_resources. Takes a configuration value egg, which is
an egg spec, and a base resource_name (default empty string)
which is the path in the egg that this starts at.
class paste.urlparser.URLParser(global_conf, directory, base_python_name, index_names=<class 'paste.urlparser.NoDefault'>, hide_extensions=<class 'paste.urlparser.NoDefault'>, ignore_extensions=<class 'paste.urlparser.NoDefault'>, constructors=None, **constructor_conf)
Application dispatching, based on URL. An instance of URLParser is
an application that loads and delegates to other applications. It
looks for files in its directory that match the first part of
PATH_INFO; these may have an extension, but are not required to have
one, in which case the available files are searched to find the
appropriate file. If it is ambiguous, a 404 is returned and an error
By default there is a constructor for .py files that loads the module,
and looks for an attribute application, which is a ready
application object, or an attribute that matches the module name,
which is a factory for building applications, and is called with no
URLParser will also look in __init__.py for special overrides.
These overrides are:
- This can modify the environment. Its return value is ignored,
and it cannot be used to change the response in any way. You
can use this, for example, to manipulate SCRIPT_NAME/PATH_INFO
(try to keep them consistent with the original URL – but
consuming PATH_INFO and moving that to SCRIPT_NAME is ok).
- urlparser_wrap(environ, start_response, app):
- After URLParser finds the application, it calls this function
(if present). If this function doesn’t call
app(environ, start_response) then the application won’t be
called at all! This can be used to allocate resources (with
try:finally:) or otherwise filter the output of the
- not_found_hook(environ, start_response):
- If no file can be found (in this directory) to match the
request, then this WSGI application will be called. You can
use this to change the URL and pass the request back to
URLParser again, or on to some other application. This
doesn’t catch all 404 Not Found responses, just missing
- application(environ, start_response):
- This basically overrides URLParser completely, and the given
application is used for all requests. urlparser_wrap and
urlparser_hook are still called, but the filesystem isn’t
searched in any way.
paste.urlparser.make_url_parser(global_conf, directory, base_python_name, index_names=None, hide_extensions=None, ignore_extensions=None, **constructor_conf)
Create a URLParser application that looks in directory, which
should be the directory for the Python package named in
base_python_name. index_names are used when viewing the
directory (like 'index' for 'index.html').
hide_extensions are extensions that are not viewable (like
'.pyc') and ignore_extensions are viewable but only if an
explicit extension is given.