Application InterfaceΒΆ
The WSGI application interface is implemented as a callable object: a function, a method, a class or an instance with an object.__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.
- return the response body to the server as strings wrapped in an iterable.
The application skeleton:
# The application interface is a callable object
def application ( # It accepts two arguments:
# environ points to a dictionary containing CGI like environment
# variables which is populated by the server for each
# received request from the client
environ,
# start_response is a callback function supplied by the server
# which takes the HTTP status and headers as arguments
start_response
):
# Build the response body possibly
# using the supplied environ dictionary
response_body = 'Request method: %s' % environ['REQUEST_METHOD']
# HTTP response code and message
status = '200 OK'
# 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.