DOKK Library

WSGI 1.0 Cheat-sheet

Authors Gustavo Narea

License CC-BY-2.0

                                                                    WSGI 1.0 Cheat-sheet
                                                               • REMOTE_USER: If the user that made the request    response headers. It takes two positional arguments: The
                    Definitions                                                                                    HTTP status string (e.g., “200 OK”) and a list made up of
                                                                 has been authenticated successfully (in this request or
 • Server: HTTP server that has Python embedded (or                                                                tuples whose first element is the header name and the
                                                                 in a previous one), this variable represents his unique
   it's itself a Python application), and calls the WSGI         user identifier (e.g., a name).                   second is the header value.
   application callable directly.
                                                               • HTTP_* variables: Those present in the HTTP       It returns a callable which can be used by legacy
 • Gateway: Server-independent Python-powered                    request, in upper case and with hyphens replaced  applications to send the body (aka write() callable), if
   application that calls the WSGI application callable          with underscores. For example, User-Agent becomes they cannot return an iterable. It can be called multiple
   directly and “connects” it to the Web server.                 HTTP_USER_AGENT.                                  times and its only argument is a string to be sent.
 • Application: The WSGI application callable.                 • wsgi.input: The file-like object that contains the        File wrapper
 • Middleware: Callable that wraps one or more WSGI              body of the request.
   applications, to filter their requests and/or responses.                                                                If the server or gateway supports a high performance
                                                               • wsgi.url_scheme: The scheme portion of the URL            method to send files, it'd be available in the WSGI
                                                                 (“http” or “https”).
         WSGI environ variables                                                                                            environ as “wsgi.file_wrapper”.
There are many variables, but the following are possibly                     API                                           It's a callable that takes one mandatory argument (the
the most commonly used ones when you want to do                                                                            file-like object whose contents should be sent) and an
something simple yet low-level without a framework:           WSGI application                                             optional size hint (the amount of bytes that should be sent
                                                              It can be any callable. It takes two positional arguments,   at the same time).
 • REQUEST_METHOD: The HTTP request method
   (e.g., “POST”, “GET”, “HEAD”, “PUT”).                      the WSGI environ and the server-provided
                                                              start_response() callable. To send a response,
 • SCRIPT_NAME: The portion of the URL path that                                                                           A file-like object in the WSGI environ where non-fatal
                                                              start_response() must be called to send the headers first
   is not consumed by the application. For example, if                                                                     errors should be written. The messages are usually sent to
                                                              and then an iterable representing the body must be
   you have Trac running on,                                                                      the server's main error log.
   the script name for Trac will always be “/trac”. If
   Trac is running on then the script     WSGI middleware                                                                      Links
   name is an empty string.                                                                                                 • Web-SIG: The best place to ask WSGI-related
                                                              WSGI middleware must be a callable with the same API
 • PATH_INFO: The portion of the URL path that is             as a WSGI application. Servers or gateways must not try         questions:
   consumed by the application. For example, if you           to distinguish an application from a piece of middleware.
   have Trac running on, the                                                               
   path info for Trac will always be everything after         WSGI environ                                                  • PEP-333:
   “/trac”. It is the path info is the part of the URL path   A dict instance. Not a dictionary-like object.
   not use by the script name.                                                                                             WSGI “shops”
 • QUERY_STRING: The URL-encoded string that                  start_response()
                                                                                                                               •   Paste:
   contains the so-called “GET arguments”. If set, it         A callable provided by the server or gateway on every
   comes after the question mark in URLs.                     request, used by the WSGI application to send its                •   Repoze:

                                   Copyright 2010 by Gustavo Narea <>. Licensed under the Creative Commons Attribution 2.0 UK: England & Wales License.