← Back to list

WSGI — Understanding Python’s Web Server Interfaces

WSGI

Smriti Rastogi in Devmap · 2026-01-13 17:25 · 4 claps · 1.7 min read
#python #wsgi #flask #software-development #production-deployment
Open on Medium ↗
Wiki topics: 🌐 · Web Development

WSGI — Understanding Python’s Web Server Interfaces

WSGI

stands for Web Server Gateway Interface. It is a standard gateway interface that defines how a python web application communicates with a web server. In simple terms it defines how the server(Nginx , Apache etc) calls your Python application.

Python frameworks like Django, Flask & FastApi provide development servers only .

Client (Browser / Mobile / API client) ↓ HTTP Web Server / Reverse Proxy (Nginx, Apache) ↓ WSGI WSGI Server (Gunicorn / uWSGI / mod_wsgi) ↓ Python call Python Web App (Flask / Django) ↑ Response

What WSGI actually is?

It defines:

  • How an HTTP request becomes a Python call
  • How a Python app returns a response
  • How environment data is passed

WSGI rule:

A Python web app must be a callable that accepts

(environ, start_response)

Step-by-step request lifecycle

  1. Client sends request

GET /users?id=1 HTTP/1.1 Host: example.com

  1. Web server receives it

Nginx: Accepts TCP connection Handles TLS, buffering, timeouts Forwards request to WSGI server

  1. WSGI server translates HTTP → Python

Gunicorn: Parses HTTP Creates a Python dictionary called environ

Example environ:

{
  'REQUEST_METHOD': 'GET',
  'PATH_INFO': '/users',
  'QUERY_STRING': 'id=1',
  'SERVER_NAME': 'example.com',
  'SERVER_PORT': '80',
  'wsgi.version': (1, 0),
  'wsgi.input': <file-like object>,
  'wsgi.errors': <file-like object>,
}
  1. WSGI server calls your app
response = app(environ, start_response)

Your Flask/Django app is just a callable.

How Flask fits into this?

When you write:

from flask import Flask
app = Flask(__name__)

Flask() creates an object of class Flask.

That class implements the __call__ method, which makes the object callable.

  • Internally, __call__ is compatible with WSGI:
# Simplified pseudo-code
class Flask:
    def __call__(self, environ, start_response):
        # Convert environ to Request object
        # Route to the right view function
        # Call start_response
        # Return response iterable
        return response_iterable
  1. Python app processes request

Framework:

  • Reads from environ
  • Routes request
  • Executes business logic
  • Prepares response
  1. App returns response

start_response is a function provided by the WSGI server to the Python application, which the app must call to begin the HTTP response.

It tells the server:

HTTP status Response headers Headers must be sent before the body

Example:

start_response(
    "200 OK",
    [("Content-Type", "application/json")]
)
return [b'{"message": "Hello"}']
  • Response body must be bytes
  • Returned as an iterable

How frameworks like Flask handle this?

Flask does the encoding for you:

from flask import Flask, jsonify
app = Flask(__name__)

@app.route("/hello")
def hello():
    return jsonify({"message": "Hello Flask"})
  • jsonify() converts Python dict → JSON string → UTF-8 bytes
  • Flask calls start_response() and returns bytes to the WSGI server
  1. WSGI server sends HTTP response

Gunicorn:

  • Converts response into HTTP
  • Sends it back to Nginx
  • Nginx sends it to client

메타데이터
post_id
5e71e9882e6d
slug
wsgi-understanding-pythons-web-server-interfaces-5e71e9882e6d
url
https://medium.com/devmap/wsgi-understanding-pythons-web-server-interfaces-5e71e9882e6d
canonical_url
https://medium.com/devmap/wsgi-understanding-pythons-web-server-interfaces-5e71e9882e6d
author_url
https://medium.com/@smritirastogi33
status
ok
fetched_at
2026-06-10 08:17:25