18.4.3 wsgiref.simple_server - a simple WSGI HTTP server

This module implements a simple HTTP server (based on BaseHTTPServer) that serves WSGI applications. Each server instance serves a single WSGI application on a given host and port. If you want to serve multiple applications on a single host and port, you should create a WSGI application that parses PATH_INFO to select which application to invoke for each request. (E.g., using the shift_path_info() function from wsgiref.util.)

make_server( host, port, app [, server_class=WSGIServer [, handler_class=WSGIRequestHandler]])
Create a new WSGI server listening on host and port, accepting connections for app. The return value is an instance of the supplied server_class, and will process requests using the specified handler_class. app must be a WSGI application object, as defined by PEP 333.

Example usage:

from wsgiref.simple_server import make_server, demo_app

httpd = make_server('', 8000, demo_app)
print "Serving HTTP on port 8000..."

# Respond to requests until process is killed

# Alternative: serve one request, then exit

demo_app( environ, start_response)
This function is a small but complete WSGI application that returns a text page containing the message ``Hello world!'' and a list of the key/value pairs provided in the environ parameter. It's useful for verifying that a WSGI server (such as wsgiref.simple_server) is able to run a simple WSGI application correctly.

class WSGIServer( server_address, RequestHandlerClass)
Create a WSGIServer instance. server_address should be a (host,port) tuple, and RequestHandlerClass should be the subclass of BaseHTTPServer.BaseHTTPRequestHandler that will be used to process requests.

You do not normally need to call this constructor, as the make_server() function can handle all the details for you.

WSGIServer is a subclass of BaseHTTPServer.HTTPServer, so all of its methods (such as serve_forever() and handle_request()) are available. WSGIServer also provides these WSGI-specific methods:

set_app( application)
Sets the callable application as the WSGI application that will receive requests.

get_app( )
Returns the currently-set application callable.

Normally, however, you do not need to use these additional methods, as set_app() is normally called by make_server(), and the get_app() exists mainly for the benefit of request handler instances.

class WSGIRequestHandler( request, client_address, server)
Create an HTTP handler for the given request (i.e. a socket), client_address (a (host,port) tuple), and server (WSGIServer instance).

You do not need to create instances of this class directly; they are automatically created as needed by WSGIServer objects. You can, however, subclass this class and supply it as a handler_class to the make_server() function. Some possibly relevant methods for overriding in subclasses:

get_environ( )
Returns a dictionary containing the WSGI environment for a request. The default implementation copies the contents of the WSGIServer object's base_environ dictionary attribute and then adds various headers derived from the HTTP request. Each call to this method should return a new dictionary containing all of the relevant CGI environment variables as specified in PEP 333.

get_stderr( )
Return the object that should be used as the wsgi.errors stream. The default implementation just returns sys.stderr.

handle( )
Process the HTTP request. The default implementation creates a handler instance using a wsgiref.handlers class to implement the actual WSGI application interface.

See About this document... for information on suggesting changes.