Web Server Gateway Interface

Jump to navigation Jump to search

The Web Server Gateway Interface (WSGI) is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language. The current version of WSGI, version 1.0.1, is specified in Python Enhancement Proposal (PEP) 3333[1].

WSGI was originally specified as PEP-333 in 2003[2]. PEP-3333, published in 2010, updates the specification for Python 3.


In 2003, Python web frameworks were typically written against only CGI, FastCGI, mod_python, or some other custom API of a specific web server.[3] To quote PEP 333:

Python currently boasts a wide variety of web application frameworks, such as Zope, Quixote, Webware, SkunkWeb, PSO, and Twisted Web -- to name just a few. This wide variety of choices can be a problem for new Python users, because generally speaking, their choice of web framework will limit their choice of usable web servers, and vice versa.

By contrast, although Java has just as many web application frameworks available, Java's "servlet" API makes it possible for applications written with any Java web application framework to run in any web server that supports the servlet API.

WSGI was thus created as an implementation-agnostic interface between web servers and web applications or frameworks to promote common ground for portable web application development[1].

Specification overview

The WSGI has two sides:

  • the server/gateway side. This is often a full web server such as Apache or Nginx, or a lightweight application server that can communicate with a webserver, such as flup.
  • the application/framework side. This is a Python callable, supplied by the Python program or framework.

Between the server and the application, there may be one or more WSGI middleware components, which implement both sides of the API, typically in Python code.

WSGI does not specify how the Python interpreter should be started, nor how the application object should be loaded or configured, and different frameworks and webservers achieve this in different ways.

WSGI Middleware

A WSGI middleware component is a Python callable that is itself a WSGI application, but may handle requests by delegating to other WSGI applications. These applications can themselves be WSGI middleware components[4].

A middleware component can perform such functions as[4]:

  • Routing a request to different application objects based on the target URL, after changing the environment variables accordingly.
  • Allowing multiple applications or frameworks to run side-by-side in the same process
  • Load balancing and remote processing, by forwarding requests and responses over a network
  • Performing content post-processing, such as applying XSLT stylesheets


Example application

A WSGI-compatible "Hello, World" application written in Python:

1 def application(environ, start_response):
2     start_response('200 OK', [('Content-Type', 'text/plain')])
3     yield b'Hello, World\n'


  • Line 1 defines a callable[5] named application, which takes two parameters, environ and start_response. environ is a dictionary containing CGI environment variables as well as other request parameters and metadata under well-defined keys[6]. start_response is a callable taking two positional parameters, status and response_headers.
  • Line 2 calls start_response, specifying "200 OK" as the HTTP status and a "Content-Type" response header.
  • Line 3 makes the callable into a generator. The body of the response is returned as an iterable of byte strings.

Example of calling an application

A full example of a WSGI network server is outside the scope of this article. Below is a sketch of how one would call a WSGI application and retrieve its HTTP status line, response headers, and response body, as Python objects[7]. Details of how to construct the environ dict have been omitted.

from io import BytesIO

def call_application(app, environ):
    status = None
    headers = None
    body = BytesIO()
    def start_response(rstatus, rheaders):
        nonlocal status, headers
        status, headers = rstatus, rheaders
    app_iter = app(environ, start_response)
        for data in app_iter:
            assert status is not None and headers is not None, \
                "start_response() was not called"
        if hasattr(app_iter, 'close'):
    return status, headers, body.getvalue()

environ = {...}  # "environ" dict
status, headers, body = call_application(app, environ)

WSGI-compatible applications and frameworks

Numerous web frameworks support WSGI:

Currently wrappers are available for FastCGI, CGI, SCGI, AJP (using flup), twisted.web, Apache (using mod_wsgi or mod_python), Nginx (using ngx_http_uwsgi_module),[22] and Microsoft IIS (using WFastCGI,[23] isapi-wsgi,[24] PyISAPIe,[25] or an ASP gateway).

See also


  1. ^ a b "PEP 3333 - Python Web Server Gateway Interface v1.0.1". Python.org. Retrieved 2018-04-04. 
  2. ^ "PEP 333 -- Python Web Server Gateway Interface v1.0". Python.org. Retrieved 2018-04-04. 
  3. ^ "FrontPage - Python Wiki". Python.org. Retrieved 2017-01-27. 
  4. ^ a b "PEP 3333 -- Python Web Server Gateway Interface v1.0.1". Python.org. Retrieved 2018-04-04. 
  5. ^ i.e. "a function, method, class, or an instance with a __call__ method"
  6. ^ "PEP 3333 -- Python Web Server Gateway Interface v1.0.1". Python.org. Retrieved 2018-04-04. 
  7. ^ "Creating WSGI Middleware - Alan Christopher Thomas - Minted - PythonKC". YouTube. 2015-08-28. Retrieved 2017-01-27. 
  8. ^ "プエラリアジェルの効果は?". Bobo.digicool.com. Retrieved 2017-01-27. 
  9. ^ "Django without mod_python, and WSGI support | Weblog | Django". Djangoproject.com. 2005-07-18. Retrieved 2017-01-27. 
  10. ^ "wsgi – WSGI server — Eventlet 0.20.1 documentation". Eventlet.net. Retrieved 2017-01-27. 
  11. ^ "Falcon - Bare-metal web API framework for Python". Retrieved 2017-10-22. 
  12. ^ "gevent-fastcgi : Python Package Index". Pypi.python.org. 2015-12-06. Retrieved 2017-01-27. 
  13. ^ "anomaly/prestans: A WSGI compliant REST micro-framework". GitHub.com. Retrieved 2017-01-27. 
  14. ^ "Google Code Archive - Long-term storage for Google Code Project Hosting". Code.google.com. Retrieved 2017-01-27. 
  15. ^ "Pycnic Framework". Pycnic.nullism.com. Retrieved 2017-01-27. 
  16. ^ "theintencity/restlite: Light-weight RESTful server tools in Python". GitHub.com. Retrieved 2017-01-27. 
  17. ^ "limodou/uliweb: Simple and easy use python web framework". GitHub.com. Retrieved 2017-01-27. 
  18. ^ "Welcome to". Web.py. 2009-09-11. Retrieved 2017-01-27. 
  19. ^ "weblayer — weblayer v0.4.3 documentation". Packages.python.org. Retrieved 2017-01-27. 
  20. ^ "Welcome | Werkzeug (The Python WSGI Utility Library)". Werkzeug.pocoo.org. Retrieved 2017-01-27. 
  21. ^ "CalDAV and CardDAV Server - A Simple Calendar and Contact Server". Radicale.org. Retrieved 2017-01-27. 
  22. ^ "Module ngx_http_uwsgi_module". Nginx.org. Retrieved 2017-01-27. 
  23. ^ "Python Tools for Visual Studio - Documentation". Pytools.codeplex.com. Retrieved 2017-01-27. 
  24. ^ "Google Code Archive - Long-term storage for Google Code Project Hosting". Code.google.com. Retrieved 2017-01-27. 
  25. ^ "Python ISAPI Extension for IIS download | SourceForge.net". Pyisapie.sourceforge.net. 2012-04-24. Retrieved 2017-01-27. 

External links