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
Bases: socket._fileobject
Faux file object attached to a socket object.
Bases: pytomo.web.wsgiserver.HTTPServer
Bases: object
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.
Bases: exceptions.Exception
Exception raised when the SSL implementation signals a fatal alert.
Bases: object
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.
alias of HTTPRequest
Bases: object
An HTTP Request (and response).
A single HTTP connection may consist of multiple request/response pairs.
If True, output will be encoded with the “chunked” transfer-coding.
This value is set automatically inside send_headers.
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.
The HTTPConnection object on which this request connected.
A dict of request headers.
A list of header tuples to write in the response.
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
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.
Assert, process, and send the HTTP response message-headers.
You must set self.status, and self.outheaders before calling this.
The HTTPServer object which is receiving this request.
Bases: object
An HTTP server.
The class to use for handling HTTP connections.
alias of HTTPConnection
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.
A Gateway instance.
Set this to an Exception instance to interrupt the server.
The maximum size, in bytes, for request bodies, or 0 for no limit.
The maximum size, in bytes, for request headers, or 0 for no limit.
The maximum number of worker threads to create (default -1 = no limit).
The minimum number of worker threads to create (default 10).
If True (the default since 3.1), sets the TCP_NODELAY socket option.
The version string to write in the Status-Line of all HTTP responses.
For example, “HTTP/1.1” is the default. This also limits the supported features used in the response.
An internal flag which marks whether the socket is accepting connections.
The ‘backlog’ arg to socket.listen(); max queued connections (default 5).
The name of the server; defaults to socket.gethostname().
The total time, in seconds, to wait for worker threads to cleanly exit.
The value to set for the SERVER_SOFTWARE entry in the WSGI environ.
If None, this defaults to '%s Server' % self.version.
An instance of SSLAdapter (or a subclass).
You must have the corresponding SSL driver library installed.
The timeout in seconds for accepted connections (default 10).
A version string for the HTTPServer.
Bases: object
Wraps a file-like object, returning an empty string when exhausted.
Bases: exceptions.Exception
Exception raised when a client speaks HTTP to an HTTPS socket.
Bases: object
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
Bases: object
Wraps a file-like object, raising MaxSizeExceeded if too large.
Bases: object
A Request Queue for the CherryPyWSGIServer which pools threads.
ThreadPool objects must provide min, get(), put(obj), start() and stop(timeout) attributes.
Number of worker threads which are idle. Read-only.
Bases: object
A WSGI dispatcher for dispatch based on the PATH_INFO.
apps: a dict or list of (path_prefix, app) pairs.
Bases: threading.Thread
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).
The current connection pulled off the Queue, or None.
A simple flag for the calling server to know when this thread has begun polling the Queue.
The HTTP Server which spawned this thread, and which owns the Queue and is placing active connections into it.
Like print_exc() but return a string. Backport for Python 2.3.
Return error numbers for all errors in errnames on this platform.
The ‘errno’ module contains different global constants depending on the specific platform (OS). This function will return the list of numeric values for a given list of potential names.
Mark the given socket fd as non-inheritable (POSIX).
Read headers from the given stream into the given header dict.
If hdict is None, a new header dict is created. Returns the populated header dict.
Headers which are repeated are folded together using a comma if their specification so dictates.
This function raises ValueError when the read bytes violate the HTTP spec. You should probably return “400 Bad Request” if this happens.