wsgi - dwilson2547/wiki_demo GitHub Wiki

What is WSGI?

WSGI (Web Server Gateway Interface) is a standard interface between web servers and Python web applications or frameworks. It was defined in PEP 3333 and is designed to improve the portability and interoperability of Python web applications.


Key Concepts of WSGI

1. Purpose

  • WSGI allows Python web applications to communicate with web servers in a standardized way.
  • It acts as a bridge, enabling developers to write web applications in Python without worrying about the specifics of the web server (e.g., Apache, Nginx).

2. How It Works

  • Application Side: Your Python web app is a callable object (a function or class with a __call__ method) that takes two arguments: environ (a dictionary containing HTTP request data) and start_response (a function to send HTTP status and headers).
  • Server Side: The web server calls this application object, passing the request data and expecting a response in the form of iterable data (e.g., a list of strings or bytes).

3. Example

Here’s a minimal WSGI application:

def application(environ, start_response):
    status = '200 OK'
    headers = [('Content-Type', 'text/plain')]
    start_response(status, headers)
    return [b"Hello, WSGI World!"]
  • When a request comes in, the server calls application(environ, start_response).
  • The application returns the response body as an iterable.

4. Middleware

  • WSGI supports middleware components that can process requests/responses between the server and the application.
  • Middleware can modify requests, add headers, or handle errors.

Why WSGI Matters

  • Flexibility: You can swap web servers or frameworks without rewriting your application.
  • Performance: WSGI servers (like Gunicorn, uWSGI, or Waitress) are optimized for production use.
  • Compatibility: Most Python web frameworks (Django, Flask, Pyramid) support WSGI.

WSGI Servers

Popular WSGI servers include:

  • Gunicorn (Green Unicorn)
  • uWSGI
  • Waitress
  • mod_wsgi (for Apache)

These servers handle HTTP requests, manage processes/threads, and call your WSGI application.


WSGI vs. ASGI

  • ASGI (Asynchronous Server Gateway Interface) is the newer standard for async Python web apps (e.g., FastAPI, Starlette).
  • WSGI is synchronous, while ASGI supports async/await.

⚠️ **GitHub.com Fallback** ⚠️