wsgiserver

A high-speed, production ready, thread pooled, generic WSGI 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!

System Message: WARNING/2 (paste.script.wsgiserver, line 11)

Block quote ends without a blank line; unexpected unindent.

']

# Here we set our application to the script_name '/' wsgi_apps = [('/', my_crazy_app)]

server = wsgiserver.CherryPyWSGIServer(('localhost', 8070), wsgi_apps,
server_name='localhost')

# Want SSL support? Just set these attributes # server.ssl_certificate = <filename> # server.ssl_private_key = <filename>

if __name__ == '__main__':
try:
server.start()
except KeyboardInterrupt:
server.stop()

This won't call the CherryPy engine (application side) at all, only the WSGI server, which is independant from the rest of CherryPy. Don't let the name "CherryPyWSGIServer" throw you; the name merely reflects its origin, not it's coupling.

The CherryPy WSGI server can serve as many WSGI applications as you want in one instance:

wsgi_apps = [('/', my_crazy_app), ('/blog', my_blog_app)]

Attributes

a SSL

None

a comma_separated_headers

['ACCEPT',
 'ACCEPT-CHARSET',
 'ACCEPT-ENCODING',
 'ACCEPT-LANGUAGE',
 'ACCEPT-RANGES',
 'ALLOW',
 'CACHE-CONTROL',
 'CONNECTION',
 'CONTENT-ENCODING',
 'CONTENT-LANGUAGE',
 'EXPECT',
 'IF-MATCH',
 'IF-NONE-MATCH',
 'PRAGMA',
 'PROXY-AUTHENTICATE',
 'TE',
 'TRAILER',
 'TRANSFER-ENCODING',
 'UPGRADE',
 'VARY',
 'VIA',
 'WARNING',
 'WWW-AUTHENTICATE']

a quoted_slash

<_sre.SRE_Pattern object at 0x8b84740>

a socket_errors_to_ignore

[32, 104, 110, 111, 112, 113, 'timed out']

Functions

f format_exc(limit=None) ...

Like print_exc() but return a string. Backport for Python 2.3.

Classes

C HTTPRequest(...) ...

An HTTP Request (and response).

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

connection: the HTTP Connection object which spawned this request. rfile: the 'read' fileobject from the connection's socket ready: when True, the request has been parsed and is ready to begin

System Message: ERROR/3 (paste.script.wsgiserver.HTTPRequest, line 6)

Unexpected indentation.
generating the response. When False, signals the calling Connection that the response should not be generated and the connection should close.

System Message: WARNING/2 (paste.script.wsgiserver.HTTPRequest, line 9)

Block quote ends without a blank line; unexpected unindent.
close_connection: signals the calling Connection that the request
should close. This does not imply an error! The client and/or server may each request that the connection be closed.
chunked_write: if True, output will be encoded with the "chunked"
transfer-coding. This value is set automatically inside send_headers.

This class contains 9 members.

C NoSSLError(...) ...

Exception raised when a client speaks HTTP to an HTTPS socket.

This class contains 6 members.

C SSL_fileobject(...) ...

Faux file object attached to a socket object.

This class contains 19 members.

C HTTPConnection(...) ...

An HTTP connection (active socket).

socket: the raw socket object (usually TCP) for this connection. addr: the "bind address" for the remote end of the socket.

System Message: ERROR/3 (paste.script.wsgiserver.HTTPConnection, line 3)

Unexpected indentation.
For IP sockets, this is a tuple of (REMOTE_ADDR, REMOTE_PORT). For UNIX domain sockets, this will be a string.

System Message: WARNING/2 (paste.script.wsgiserver.HTTPConnection, line 5)

Block quote ends without a blank line; unexpected unindent.
server: the HTTP Server for this Connection. Usually, the server
object possesses a passive (server) socket which spawns multiple, active (client) sockets, one for each connection.

environ: a WSGI environ template. This will be copied for each request. rfile: a fileobject for reading from the socket. sendall: a function for writing (+ flush) to the socket.

This class contains 6 members.

C WorkerThread(...) ...

Thread which continuously polls a Queue for Connection objects.

server: the HTTP Server which spawned this thread, and which owns the
Queue and is placing active connections into it.
ready: a simple flag for the calling server to know when this thread
has begun polling the Queue.

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).

This class contains 2 members.

C SSLConnection(...) ...

A thread-safe wrapper for an SSL.Connection.

*args: the arguments to create the wrapped SSL.Connection(*args).

System Message: WARNING/2 (paste.script.wsgiserver.SSLConnection, line 1); backlink

Inline emphasis start-string without end-string.

System Message: WARNING/2 (paste.script.wsgiserver.SSLConnection, line 1); backlink

Inline emphasis start-string without end-string.

This class contains 35 members.

C CherryPyWSGIServer(...) ...

An HTTP server for WSGI.

bind_addr: a (host, port) tuple if TCP sockets are desired;
for UNIX sockets, supply the filename as a string.
wsgi_app: the WSGI 'application callable'; multiple WSGI applications
may be passed as (script_name, callable) pairs.

System Message: WARNING/2 (paste.script.wsgiserver.CherryPyWSGIServer, line 5)

Definition list ends without a blank line; unexpected unindent.

numthreads: the number of worker threads to create (default 10). server_name: the string to set for WSGI's SERVER_NAME environ entry.

System Message: ERROR/3 (paste.script.wsgiserver.CherryPyWSGIServer, line 7)

Unexpected indentation.
Defaults to socket.gethostname().

System Message: WARNING/2 (paste.script.wsgiserver.CherryPyWSGIServer, line 8)

Block quote ends without a blank line; unexpected unindent.

max: the maximum number of queued requests (defaults to -1 = no limit). request_queue_size: the 'backlog' argument to socket.listen();

System Message: ERROR/3 (paste.script.wsgiserver.CherryPyWSGIServer, line 10)

Unexpected indentation.
specifies the maximum number of queued connections (default 5).

System Message: WARNING/2 (paste.script.wsgiserver.CherryPyWSGIServer, line 11)

Block quote ends without a blank line; unexpected unindent.

timeout: the timeout in seconds for accepted connections (default 10).

protocol: the version string to write in the Status-Line of all
HTTP responses. For example, "HTTP/1.1" (the default). This also limits the supported features used in the response.

SSL/HTTPS

The OpenSSL module must be importable for SSL functionality. You can obtain it from http://pyopenssl.sourceforge.net/

ssl_certificate: the filename of the server SSL certificate. ssl_privatekey: the filename of the server's private key file.

If either of these is None (both are None by default), this server will not use SSL. If both are given and are valid, they will be read on server start and used in the SSL context for the listening socket.

This class contains 13 members.

See the source for more information.