WSGI Environ Required Keys in Python
The Python Web Server Gateway Interface (WSGI), defined in PEP 333
and updated in PEP 3333, acts as a standard interface between web
servers and Python web applications or frameworks. When a request
arrives, the WSGI server passes a dictionary called environ
containing request metadata, configuration parameters, and I/O streams.
This article outlines the specific key-value pairs that are guaranteed
to exist within that environ dictionary for every
conforming WSGI implementation.
WSGI-Specific Keys
PEP 3333 mandates eight WSGI-specific keys. These keys always begin
with the wsgi. prefix and define the runtime environment,
streaming interfaces, and concurrency capabilities:
wsgi.version: A tuple of two integers,(1, 0), representing WSGI version 1.0.wsgi.url_scheme: A string representing the URL scheme used to access the application, typically"http"or"https".wsgi.input: An input stream (a file-like object supporting.read(),.readline(), etc.) from which the HTTP request body can be read.wsgi.errors: An output stream (a file-like object supporting.write(),.writelines(), and.flush()) designated for logging application errors.wsgi.multithread: A boolean that evaluates toTrueif the application object may be simultaneously invoked by another thread in the same process, andFalseotherwise.wsgi.multiprocess: A boolean that evaluates toTrueif an equivalent application object may be simultaneously invoked by another process, andFalseotherwise.wsgi.run_once: A boolean that evaluates toTrueif the server or gateway expects the application to be invoked only once during the lifetime of its containing process (common in traditional CGI environments).
Mandatory CGI Keys
WSGI incorporates variables derived from the Common Gateway Interface
(CGI) specification. While some CGI variables are optional (such as
CONTENT_TYPE or REMOTE_USER, which only exist
if the client provided them), the following CGI keys are strictly
required to be present in every environ dictionary:
REQUEST_METHOD: A string representing the HTTP request method, such as"GET","POST","PUT", or"DELETE". This string cannot be empty.SCRIPT_NAME: A string representing the initial portion of the URL path that corresponds to the application object, allowing the application to know its prefix. This may be an empty string""if the application sits at the server root.PATH_INFO: A string representing the remainder of the request URL's path within the application. This value can be an empty string if the request path matchesSCRIPT_NAMEand has no trailing slash.QUERY_STRING: A string containing the portion of the request URL that follows the?character. If no query string was sent, this key must still be present as an empty string"".SERVER_NAME: A string specifying the hostname or IP address of the server responding to the request.SERVER_PORT: A string representing the TCP port number on which the server is listening (e.g.,"80","443").SERVER_PROTOCOL: A string defining the version of the protocol used by the client to make the request, typically"HTTP/1.0"or"HTTP/1.1".
Summary
Any WSGI-compliant server (such as Gunicorn, uWSGI, or the standard
library's wsgiref) must populate these 15 keys for every
incoming request. Applications can safely access these keys using direct
dictionary indexing (environ['KEY']) without handling a
KeyError.