All topics / WSGI & ASGI Explained

WSGI & ASGI Explained

Learn the protocol every Python web framework sits on: what WSGI is and the problem it solved, a bare WSGI app with no framework, WSGI servers and middleware, why ASGI exists for async, a bare ASGI app and its servers, and how Flask, Django, and FastAPI are all built on these contracts. The bottom layer, made visible.

  1. What WSGI Is WSGI is the standard contract between a Python web server and a Python web app — a callable taking (environ, start_response). Write to it once, run on any compliant server.
  2. A WSGI App From Scratch Write a real web app in a dozen lines with no framework — a plain WSGI callable that reads the request from environ, routes by hand, and returns bytes — then see exactly what Flask adds on top.
  3. The WSGI Server & Middleware The dev server isn't enough for production. gunicorn and friends import your WSGI app and run worker processes to serve it, and middleware is a WSGI app that wraps your WSGI app — the root of framework middleware.
  4. Why ASGI Exists WSGI's synchronous worker blocks for a whole request and can't do websockets at all. ASGI fixes both with an async, event-based contract built for high I/O concurrency and long-lived connections.
  5. An ASGI App & the Servers Write a bare ASGI app with scope/receive/send, read the request, run it under uvicorn, glimpse lifespan and websockets, then see FastAPI as ergonomics over this exact machinery.
  6. From Protocol to Framework The payoff: see the WSGI or ASGI callable under every Python web framework. Flask, Django, FastAPI, routing, request objects, middleware, and the deploy story all map back to one small contract.