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:

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:

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.