The wsgiserver module is accessible via the paste.script module.
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!
']
# 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)]
['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']
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
generating the response. When False, signals the calling Connection that the response should not be generated and the connection should close.
This class contains 9 members.
Exception raised when a client speaks HTTP to an HTTPS socket.
This class contains 6 members.
Faux file object attached to a socket object.
This class contains 19 members.
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.
For IP sockets, this is a tuple of (REMOTE_ADDR, REMOTE_PORT). For UNIX domain sockets, this will be a string.
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.
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).
This class contains 2 members.
A thread-safe wrapper for an SSL.Connection.
*args: the arguments to create the wrapped SSL.Connection(*args).
This class contains 35 members.
An HTTP server for WSGI.
numthreads: the number of worker threads to create (default 10). server_name: the string to set for WSGI's SERVER_NAME environ entry.
Defaults to socket.gethostname().
max: the maximum number of queued requests (defaults to -1 = no limit). request_queue_size: the 'backlog' argument to socket.listen();
specifies the maximum number of queued connections (default 5).
timeout: the timeout in seconds for accepted connections (default 10).
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.