cherrypy._cpwsgi module

WSGI interface (see PEP 333 and 3333).

Note that WSGI environ keys and values are ‘native strings’; that is, whatever the type of “” is. For Python 2, that’s a byte string; for Python 3, it’s a unicode string. But PEP 3333 says: “even if Python’s str type is actually Unicode “under the hood”, the content of native strings must still be translatable to bytes via the Latin-1 encoding!”

class cherrypy._cpwsgi.AppResponse(environ, start_response, cpapp)[source]

Bases: object

WSGI response iterable for CherryPy applications.

close()[source]

Close and de-reference the current request and response.

(Core)

headerNames = {'CONTENT_LENGTH': 'Content-Length', 'CONTENT_TYPE': 'Content-Type', 'HTTP_CGI_AUTHORIZATION': 'Authorization', 'REMOTE_ADDR': 'Remote-Addr', 'REMOTE_HOST': 'Remote-Host'}
recode_path_qs(path, qs)[source]
run()[source]

Create a Request object using environ.

translate_headers(environ)[source]

Translate CGI-environ header names to HTTP header names.

class cherrypy._cpwsgi.CPWSGIApp(cpapp, pipeline=None)[source]

Bases: object

A WSGI application object for a CherryPy Application.

config = {}

A dict whose keys match names listed in the pipeline.

Each value is a further dict which will be passed to the corresponding named WSGI callable (from the pipeline) as keyword arguments.

head = None

Rather than nest all apps in the pipeline on each call, it’s only done the first time, and the result is memoized into self.head. Set this to None again if you change self.pipeline after calling self.

namespace_handler(k, v)[source]

Config handler for the ‘wsgi’ namespace.

pipeline = [('ExceptionTrapper', <class 'cherrypy._cpwsgi.ExceptionTrapper'>), ('InternalRedirector', <class 'cherrypy._cpwsgi.InternalRedirector'>)]

A list of (name, wsgiapp) pairs.

Each ‘wsgiapp’ MUST be a constructor that takes an initial, positional ‘nextapp’ argument, plus optional keyword arguments, and returns a WSGI application (that takes environ and start_response arguments). The ‘name’ can be any you choose, and will correspond to keys in self.config.

response_class

The class to instantiate and return as the next app in the WSGI chain.

alias of AppResponse

tail(environ, start_response)[source]

WSGI application callable for the actual CherryPy application.

You probably shouldn’t call this; call self.__call__ instead, so that any WSGI middleware in self.pipeline can run first.

class cherrypy._cpwsgi.ExceptionTrapper(nextapp, throws=(<class 'KeyboardInterrupt'>, <class 'SystemExit'>))[source]

Bases: object

WSGI middleware that traps exceptions.

class cherrypy._cpwsgi.InternalRedirector(nextapp, recursive=False)[source]

Bases: object

WSGI middleware that handles raised cherrypy.InternalRedirect.

class cherrypy._cpwsgi.VirtualHost(default, domains=None, use_x_forwarded_host=True)[source]

Bases: object

Select a different WSGI application based on the Host header.

This can be useful when running multiple sites within one CP server. It allows several domains to point to different applications. For example:

root = Root()
RootApp = cherrypy.Application(root)
Domain2App = cherrypy.Application(root)
SecureApp = cherrypy.Application(Secure())

vhost = cherrypy._cpwsgi.VirtualHost(
    RootApp,
    domains={
        'www.domain2.example': Domain2App,
        'www.domain2.example:443': SecureApp,
    },
)

cherrypy.tree.graft(vhost)
default = None

Required.

The default WSGI application.

domains = {}

application} pairs.

The incoming “Host” request header is looked up in this dict, and, if a match is found, the corresponding WSGI application will be called instead of the default. Note that you often need separate entries for “example.com” and “www.example.com”. In addition, “Host” headers may contain the port number.

Type:

A dict of {host header value

use_x_forwarded_host = True

If True (the default), any “X-Forwarded-Host” request header will be used instead of the “Host” header. This is commonly added by HTTP servers (such as Apache) when proxying.

class cherrypy._cpwsgi._TrappedResponse(nextapp, environ, start_response, throws)[source]

Bases: object

close()[source]
response = <list_iterator object>
trap(func, *args, **kwargs)[source]
cherrypy._cpwsgi.downgrade_wsgi_ux_to_1x(environ)[source]

Return a new environ dict for WSGI 1.x from the given WSGI u.x environ.