Understanding WSGI: the request/response contract between Python apps and servers
WSGI (Web Server Gateway Interface) is the standard call contract through which Python web applications and WSGI servers exchange HTTP requests. An app is a synchronous callable that receives environ and startresponse, then returns an iterable of response bytes. Understanding this boundary helps you connect a framework's application object to a server and distinguish WSGI from ASGI. This article explains WSGI 1.0.1 in PEP 3333 and compares its core behavior with the current ASGI specification; it is not a deployment guide for a particular framework or server.
WSGI (Web Server Gateway Interface) is the standard call contract through which Python web applications and WSGI servers exchange HTTP requests. An app is a synchronous callable that receives environ and start_response, then returns an iterable of response bytes. Understanding this boundary helps you connect a framework's application object to a server and distinguish WSGI from ASGI. This article explains WSGI 1.0.1 in PEP 3333 and compares its core behavior with the current ASGI specification; it is not a deployment guide for a particular framework or server.
From one request to a response iterable
For each request, a WSGI server prepares an environment dictionary and a response-start callable, then calls the app. The environment contains CGI and WSGI values such as REQUEST_METHOD, PATH_INFO, QUERY_STRING, wsgi.url_scheme, and wsgi.input. The latter is a stream for reading the request body. Values that do not exist, such as REMOTE_USER before authentication, may be omitted.
def application(environ, start_response):
body = b"Hello from WSGI\n"
start_response(
"200 OK",
[
("Content-Type", "text/plain; charset=utf-8"),
("Content-Length", str(len(body))),
],
)
return [body]This is a toy example of the contract; it was not run or verified on a production server. A WSGI app takes two positional arguments. It passes a status string and a list of (name, value) header pairs to start_response, then returns an iterable that yields bytes. Returning Python 3 str as the response body does not conform. A generator can produce a response in chunks. If the iterable has close(), the server must call it when the request finishes or the client disconnects early.
Calling start_response does not mean the headers must be sent immediately. A server may wait for the first non-empty body bytes or for the iterable to finish. This leaves room to replace a response if an error occurs while the body is being generated. Streaming or large-file responses can avoid holding the entire output in memory, but the iterable may not be consumed to completion after an early disconnect, so resource cleanup matters.
The boundary between framework and server
A framework usually creates a WSGI callable. For example, a Django project's wsgi.py exposes an application object that a WSGI server such as Gunicorn imports. The real import path must match the project modules; a framework name or development-server address alone does not prove that the server loaded the intended app.
Check three parts separately:
**Callable:** can the server import the WSGI object at the configured module path?
**Request input:** does the app read method, path, query and body as expected? The body is read as bytes from `wsgi.input`.
**Response output:** does the app set status and headers through `start_response` and yield only bytes? An empty iterable can be a valid response.
WSGI defines an app/server interface, not worker counts, TLS, restarts, host binding or thread models. Those behaviors belong to the selected server, proxy, operating system and service manager.
Choose between WSGI and ASGI
WSGI is a synchronous, single-call interface. It fits traditional request/response applications and many established synchronous frameworks. ASGI uses an asynchronous callable application(scope, receive, send) and exchanges connection details and events, which can support protocols with multiple events during one connection, such as WebSocket.
| Criterion | WSGI | ASGI |
|---|---|---|
| App call | Synchronous (environ, start_response) | Asynchronous (scope, receive, send) |
| Response | Iterable of bytes | Explicit event messages sent and received |
| Typical fit | Synchronous request/response apps and legacy compatibility | Async code and multi-event connections such as WebSocket |
| Conversion | Run directly on a WSGI server | Run directly on an ASGI server; a compatibility wrapper can run synchronous WSGI in a thread pool |
Using a little async syntax does not automatically give a WSGI app asynchronous throughput. Before a migration, check framework and middleware support, synchronous libraries, long-lived connections, and the wrapper's execution model. ASGI defines a WSGI compatibility path, but that translates the interface; it does not rewrite the app as asynchronous code.
Distinguish common errors
**The server cannot import `application`:** check the project root, module path, callable name, and virtual-environment dependencies.
**The response body has the wrong type:** Python 3 WSGI bodies must be `bytes`. Encode text explicitly before returning it.
**Headers or an error page behave unexpectedly:** distinguish the `start_response` call from the first body byte. Once part of a response is sent, changing its status or headers is difficult.
**A WebSocket is being connected to a WSGI app:** WSGI does not define WebSocket event exchange. Use an ASGI app/server path that supports it.
**The app path is correct but external behavior differs:** WSGI itself does not configure proxy trust, TLS, workers, or zero-downtime deployment; inspect the server and deployment separately.
Sources and scope
[PEP 3333: WSGI 1.0.1](https://peps.python.org/pep-3333/) — callable signature, `environ`, `start_response`, byte iterable, delayed headers, and cleanup
[ASGI introduction](https://asgi.readthedocs.io/en/stable/introduction.html) — ASGI callable, events, multi-event protocols, and WSGI compatibility
[ASGI specification](https://asgi.readthedocs.io/en/stable/specs/main.html) — ASGI connection scope and receive/send contract
[Django WSGI deployment](https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/) — framework-provided callable and server connection concept
The specifications and official docs were checked on 2026-11-19. The code is illustrative and was not executed. This article makes no claim about the performance, security, or deployment outcome of a particular server or framework.