cherrypy.wsgiserver – WSGI Server

A high-speed, production ready, thread pooled, generic HTTP server.

Simplest example on how to use this module directly (without using CherryPy’s application machinery):

from cherrypy import wsgiserver

def my_crazy_app(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type','text/plain')]
    start_response(status, response_headers)
    return ['Hello world!']

server = wsgiserver.CherryPyWSGIServer(
            ('0.0.0.0', 8070), my_crazy_app,
            server_name='www.cherrypy.example')
server.start()

The CherryPy WSGI server can serve as many WSGI applications as you want in one instance by using a WSGIPathInfoDispatcher:

d = WSGIPathInfoDispatcher({'/': my_crazy_app, '/blog': my_blog_app})
server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 80), d)

Want SSL support? Just set server.ssl_adapter to an SSLAdapter instance.

This won’t call the CherryPy engine (application side) at all, only the HTTP server, which is independent from the rest of CherryPy. Don’t let the name “CherryPyWSGIServer” throw you; the name merely reflects its origin, not its coupling.

For those of you wanting to understand internals of this module, here’s the basic call flow. The server’s listening thread runs a very tight loop, sticking incoming connections onto a Queue:

server = CherryPyWSGIServer(...)
server.start()
while True:
    tick()
    # This blocks until a request comes in:
    child = socket.accept()
    conn = HTTPConnection(child, ...)
    server.requests.put(conn)

Worker threads are kept in a pool and poll the Queue, popping off and then handling each connection in turn. Each connection can consist of an arbitrary number of requests and their responses, so we run a nested loop:

while True:
    conn = server.requests.get()
    conn.communicate()
    ->  while True:
            req = HTTPRequest(...)
            req.parse_request()
            ->  # Read the Request-Line, e.g. "GET /page HTTP/1.1"
                req.rfile.readline()
                read_headers(req.rfile, req.inheaders)
            req.respond()
            ->  response = app(...)
                try:
                    for chunk in response:
                        if chunk:
                            req.write(chunk)
                finally:
                    if hasattr(response, "close"):
                        response.close()
            if req.close_connection:
                return

HTTP

class cherrypy.wsgiserver.HTTPRequest(server, conn)

An HTTP Request (and response).

A single HTTP connection may consist of multiple request/response pairs.

parse_request()
Parse the next HTTP request start-line and message-headers.
parse_request_uri(uri)

Parse a Request-URI into (scheme, authority, path).

Note that Request-URI’s must be one of:

Request-URI    = "*" | absoluteURI | abs_path | authority

Therefore, a Request-URI which starts with a double forward-slash cannot be a “net_path”:

net_path      = "//" authority [ abs_path ]

Instead, it must be interpreted as an “abs_path” with an empty first path segment:

abs_path      = "/"  path_segments
path_segments = segment *( "/" segment )
segment       = *pchar *( ";" param )
param         = *pchar
read_request_headers()
Read self.rfile into self.inheaders. Return success.
respond()
Call the gateway and write its iterable output.
send_headers()

Assert, process, and send the HTTP response message-headers.

You must set self.status, and self.outheaders before calling this.

simple_response(status, msg='')
Write a simple response back to the client.
write(chunk)
Write unbuffered data to the client.
class cherrypy.wsgiserver.HTTPConnection(server, sock, makefile=<class 'cherrypy.wsgiserver.CP_fileobject'>)

An HTTP connection (active socket).

server: the Server object which received this connection. socket: the raw socket object (usually TCP) for this connection. makefile: a fileobject class for reading from the socket.

RequestHandlerClass
alias of HTTPRequest
close()
Close the socket underlying this connection.
communicate()
Read each request and respond appropriately.
class cherrypy.wsgiserver.HTTPServer(bind_addr, gateway, minthreads=10, maxthreads=-1, server_name=None)

An HTTP server.

ConnectionClass
alias of HTTPConnection
bind(family, type, proto=0)
Create (or recreate) the actual socket object.
bind_addr

The interface on which to listen for connections.

For TCP sockets, a (host, port) tuple. Host values may be any IPv4 or IPv6 address, or any valid hostname. The string ‘localhost’ is a synonym for ‘127.0.0.1’ (or ‘::1’, if your hosts file prefers IPv6). The string ‘0.0.0.0’ is a special IPv4 entry meaning “any active interface” (INADDR_ANY), and ‘::’ is the similar IN6ADDR_ANY for IPv6. The empty string or None are not allowed.

For UNIX sockets, supply the filename as a string.

interrupt
Set this to an Exception instance to interrupt the server.
start()
Run the server forever.
stop()
Gracefully shutdown a server that is serving forever.
tick()
Accept a new connection and put it on the Queue.

Request Entities

class cherrypy.wsgiserver.SizeCheckWrapper(rfile, maxlen)
Wraps a file-like object, raising MaxSizeExceeded if too large.
class cherrypy.wsgiserver.KnownLengthRFile(rfile, content_length)
Wraps a file-like object, returning an empty string when exhausted.
class cherrypy.wsgiserver.ChunkedRFile(rfile, maxlen, bufsize=8192)

Wraps a file-like object, returning an empty string when exhausted.

This class is intended to provide a conforming wsgi.input value for request entities that have been encoded with the ‘chunked’ transfer encoding.

class cherrypy.wsgiserver.CP_fileobject(*args, **kwargs)

Faux file object attached to a socket object.

sendall(data)
Sendall for non-blocking sockets.

Exceptions

class cherrypy.wsgiserver.MaxSizeExceeded
class cherrypy.wsgiserver.NoSSLError
Exception raised when a client speaks HTTP to an HTTPS socket.
class cherrypy.wsgiserver.FatalSSLAlert
Exception raised when the SSL implementation signals a fatal alert.

Thread Pool

class cherrypy.wsgiserver.WorkerThread(server)

Thread which continuously polls a Queue for Connection objects.

Due to the timing issues of polling a Queue, a WorkerThread does not check its own ‘ready’ flag after it has started. To stop the thread, it is necessary to stick a _SHUTDOWNREQUEST object onto the Queue (one for each running WorkerThread).

class cherrypy.wsgiserver.ThreadPool(server, min=10, max=-1)

A Request Queue for the CherryPyWSGIServer which pools threads.

ThreadPool objects must provide min, get(), put(obj), start() and stop(timeout) attributes.

grow(amount)
Spawn new worker threads (not above self.max).
idle
Number of worker threads which are idle. Read-only.
shrink(amount)
Kill off worker threads (not below self.min).
start()
Start the pool of threads.

SSL

class cherrypy.wsgiserver.SSLAdapter(certificate, private_key, certificate_chain=None)

Base class for SSL driver library adapters.

Required methods:

  • wrap(sock) -> (wrapped socket, ssl environ dict)
  • makefile(sock, mode='r', bufsize=DEFAULT_BUFFER_SIZE) -> socket file object

WSGI

class cherrypy.wsgiserver.CherryPyWSGIServer(bind_addr, wsgi_app, numthreads=10, server_name=None, max=-1, request_queue_size=5, timeout=10, shutdown_timeout=5)
class cherrypy.wsgiserver.Gateway(req)
class cherrypy.wsgiserver.WSGIGateway(req)
get_environ()
Return a new environ dict targeting the given wsgi.version
start_response(status, headers, exc_info=None)
WSGI callable to begin the HTTP response.
write(chunk)

WSGI callable to write unbuffered data to the client.

This method is also used internally by start_response (to write data from the iterable returned by the WSGI application).

class cherrypy.wsgiserver.WSGIGateway_10(req)
get_environ()
Return a new environ dict targeting the given wsgi.version
class cherrypy.wsgiserver.WSGIGateway_u0(req)
get_environ()
Return a new environ dict targeting the given wsgi.version
class cherrypy.wsgiserver.WSGIPathInfoDispatcher(apps)

A WSGI dispatcher for dispatch based on the PATH_INFO.

apps: a dict or list of (path_prefix, app) pairs.

Table Of Contents

Previous topic

cherrypy.wsgiserver

Next topic

cherrypy.wsgiserver.ssl_builtin – Builtin SSL

This Page