Application Interface
The WSGI application interface is implemented as a callable object: a function, a method, a class or an instance with a __call__ method. That callable
-
must accept two positional parameters:
- A dictionary containing CGI like variables; and
- a callback function that will be used by the application to send HTTP status code/message and HTTP headers to the server.
- and must return the response body to the server as strings wrapped in an iterable.
The application skeleton:
# This is our application object. It could have any name, # except when using mod_wsgi where it must be "application" def application( # It accepts two arguments: # environ points to a dictionary containing CGI like environment variables # which is filled by the server for each received request from the client environ, # start_response is a callback function supplied by the server # which will be used to send the HTTP status and headers to the server start_response): # build the response body possibly using the environ dictionary response_body = 'The request method was %s' % environ['REQUEST_METHOD'] # HTTP response code and message status = '200 OK' # These are HTTP headers expected by the client. # They must be wrapped as a list of tupled pairs: # [(Header name, Header value)]. response_headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(response_body)))] # Send them to the server using the supplied function start_response(status, response_headers) # Return the response body. # Notice it is wrapped in a list although it could be any iterable. return [response_body]
This code will not run because it lacks the server instantiation step. Next page will show how to do it.