cherrypy._cpwsgi

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!”

Classes

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

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 = {}

A dict of {host header value: 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.

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.InternalRedirector(nextapp, recursive=False)

WSGI middleware that handles raised cherrypy.InternalRedirect.

class cherrypy._cpwsgi.ExceptionTrapper(nextapp, throws=(<type 'exceptions.KeyboardInterrupt'>, <type 'exceptions.SystemExit'>))

WSGI middleware that traps exceptions.

class cherrypy._cpwsgi.AppResponse(environ, start_response, cpapp)

WSGI response iterable for CherryPy applications.

close()

Close and de-reference the current request and response. (Core)

run()

Create a Request object using environ.

translate_headers(environ)

Translate CGI-environ header names to HTTP header names.

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

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)

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)

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.