Package wsgiwapi :: Package cpwsgiserver
[frames] | no frames]

Package cpwsgiserver

source code

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!
']
    
    server = wsgiserver.CherryPyWSGIServer(
                ('0.0.0.0', 8070), my_crazy_app,
                server_name='www.cherrypy.example')
    
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 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 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()
                    req.read_headers()
                req.respond()
                ->  response = wsgi_app(...)
                    try:
                        for chunk in response:
                            if chunk:
                                req.write(chunk)
                    finally:
                        if hasattr(response, "close"):
                            response.close()
                if req.close_connection:
                    return

Classes
  WSGIPathInfoDispatcher
A WSGI dispatcher for dispatch based on the PATH_INFO.
  MaxSizeExceeded
  SizeCheckWrapper
Wraps a file-like object, raising MaxSizeExceeded if too large.
  HTTPRequest
An HTTP Request (and response).
  NoSSLError
Exception raised when a client speaks HTTP to an HTTPS socket.
  FatalSSLAlert
Exception raised when the SSL implementation signals a fatal alert.
  CP_fileobject
Faux file object attached to a socket object.
  SSL_fileobject
SSL file object attached to a socket object.
  HTTPConnection
An HTTP connection (active socket).
  WorkerThread
Thread which continuously polls a Queue for Connection objects.
  ThreadPool
A Request Queue for the CherryPyWSGIServer which pools threads.
  SSLConnection
A thread-safe wrapper for an SSL.Connection.
  CherryPyWSGIServer
An HTTP server for WSGI.
Functions
 
plat_specific_errors(*errnames)
Return error numbers for all errors in errnames on this platform.
source code
 
format_exc(limit=None)
Like print_exc() but return a string.
source code
 
prevent_socket_inheritance(sock)
Mark the given socket fd as non-inheritable (POSIX).
source code
Variables
  quoted_slash = re.compile("(?i)%2F")
  SSL = None
  socket_error_eintr = plat_specific_errors("EINTR", "WSAEINTR")
  socket_errors_to_ignore = plat_specific_errors("EPIPE", "EBADF...
  socket_errors_nonblocking = plat_specific_errors('EAGAIN', 'EW...
  comma_separated_headers = ['ACCEPT', 'ACCEPT-CHARSET', 'ACCEPT...
Function Details

plat_specific_errors(*errnames)

source code 

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.

format_exc(limit=None)

source code 

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


Variables Details

socket_errors_to_ignore

Value:
plat_specific_errors("EPIPE", "EBADF", "WSAEBADF", "ENOTSOCK", "WSAENO\
TSOCK", "ETIMEDOUT", "WSAETIMEDOUT", "ECONNREFUSED", "WSAECONNREFUSED"\
, "ECONNRESET", "WSAECONNRESET", "ECONNABORTED", "WSAECONNABORTED", "E\
NETRESET", "WSAENETRESET", "EHOSTDOWN", "EHOSTUNREACH",)

socket_errors_nonblocking

Value:
plat_specific_errors('EAGAIN', 'EWOULDBLOCK', 'WSAEWOULDBLOCK')

comma_separated_headers

Value:
['ACCEPT', 'ACCEPT-CHARSET', 'ACCEPT-ENCODING', 'ACCEPT-LANGUAGE', 'AC\
CEPT-RANGES', 'ALLOW', 'CACHE-CONTROL', 'CONNECTION', 'CONTENT-ENCODIN\
G', 'CONTENT-LANGUAGE', 'EXPECT', 'IF-MATCH', 'IF-NONE-MATCH', 'PRAGMA\
', 'PROXY-AUTHENTICATE', 'TE', 'TRAILER', 'TRANSFER-ENCODING', 'UPGRAD\
E', 'VARY', 'VIA', 'WARNING', 'WWW-AUTHENTICATE']