Web Server Gateway Interface

Short description: Calling convention for web servers to forward requests to web applications written in Python

## Contents

* 1 Background
* 2 Specification overview
* 3 WSGI middleware
* 4 Examples
* 4.1 Example application
* 4.2 Example of calling an application
* 5 WSGI-compatible applications and frameworks
* 6 See also
* 7 References
* 8 External links

The Web Server Gateway Interface (WSGI, pronounced whiskey[1][2] or WIZ-ghee[3]) 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.[4]

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

## Background

In 2003, Python web frameworks were typically written against only CGI, FastCGI, mod python, or some other custom API of a specific web server.[6] 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-neutral interface between web servers and web applications or frameworks to promote common ground for portable web application development.[4]

## Specification overview

The WSGI has two sides:

* the server/gateway side. This is often running full web server software such as Apache or Nginx, or is 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.[7]

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

* 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

## Examples

### Example application

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

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

Where:

* Line 1 defines a function[8] 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.[9] `start_response` is a callable itself, 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 function 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.[10] 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)
try:
for data in app_iter:
assert status is not None and headers is not None, \
"start_response() was not called"
body.write(data)
finally:
if hasattr(app_iter, 'close'):
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:

* bjoern
* BlueBream
* bobo[11]
* Bottle
* CherryPy
* Django[12]
* Eventlet[13]
* FastWSGI
* Flask
* Falcon (web framework) [14]
* Gevent-FastCGI[15]
* Google App Engine's webapp2
* Gunicorn
* prestans[16]
* mod wsgi for use with Apache[17]
* netius
* pycnic[18]
* Paste component WebOb is specifically a WSGI extension. It was adopted by the Pylons project.
* Pylons
* Pyramid
* restlite[19]
* Tornado
* Trac
* TurboGears
* Uliweb[20]
* uWSGI
* Waitress[21]
* web.py[22]
* web2py
* weblayer[23]
* Werkzeug[24]
* Radicale[25]

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),[26] Nginx Unit (using the Python language module),[27] and Microsoft IIS (using WFastCGI,[28] isapi-wsgi,[29] PyISAPIe,[30] or an ASP gateway).

## See also

* Asynchronous Server Gateway Interface (ASGI) – The spiritual successor to WSGI, adding support for asynchronous applications
* Rack – Ruby web server interface
* PSGI – Perl Web Server Gateway Interface
* SCGI – Simple Common Gateway Interface
* JSGI – JavaScript web server gateway interface

## References

1. ↑ Simionato, Michele (June 11, 2007). "An Introduction to Web Programming with WSGI". http://www.phyast.pitt.edu/~micheles/python/europython07/talk.html.
2. ↑ Edge, Jake (July 9, 2019). "Mucking about with microframeworks". LWN. https://lwn.net/Articles/792882/.
3. ↑ Goldberg, Kevin (2016-05-09). "An Introduction to Python WSGI Servers for Performance | AppDynamics" (in en-US). https://www.appdynamics.com/blog/engineering/an-introduction-to-python-wsgi-servers-part-1/.
4. ↑ 4.0 4.1 "PEP 3333 - Python Web Server Gateway Interface v1.0.1". https://www.python.org/dev/peps/pep-3333/.
5. ↑ "PEP 333 -- Python Web Server Gateway Interface v1.0" (in en). https://www.python.org/dev/peps/pep-0333/.
6. ↑ "FrontPage - Python Wiki". https://www.python.org/cgi-bin/moinmoin/WebProgramming.
7. ↑ 7.0 7.1 "PEP 3333 -- Python Web Server Gateway Interface v1.0.1" (in en). https://www.python.org/dev/peps/pep-3333/#middleware-components-that-play-both-sides.
8. ↑ i.e. "a function, method, class, or an instance with a `__call__` method"
9. ↑ "PEP 3333 -- Python Web Server Gateway Interface v1.0.1" (in en). https://www.python.org/dev/peps/pep-3333/#environ-variables.
10. ↑ "Creating WSGI Middleware - Alan Christopher Thomas - Minted - PythonKC". YouTube. 2015-08-28. https://www.youtube.com/watch?v=afnDANxsaYo.
11. ↑ "プエラリアジェルの効果は?". http://bobo.digicool.com.
12. ↑ "Django without mod_python, and WSGI support | Weblog | Django". 2005-07-18. http://www.djangoproject.com/weblog/2005/jul/18/local_server/.
13. ↑ "wsgi – WSGI server — Eventlet 0.20.1 documentation". http://eventlet.net/doc/modules/wsgi.html.
14. ↑ "Falcon - Bare-metal web API framework for Python". https://falconframework.org.
15. ↑ "gevent-fastcgi 1.0.2.1 : Python Package Index". 2015-12-06. https://pypi.python.org/pypi/gevent-fastcgi.
16. ↑ "anomaly/prestans: A WSGI compliant REST micro-framework". https://github.com/prestans/prestans/.
17. ↑ "Google Code Archive - Long-term storage for Google Code Project Hosting". http://code.google.com/p/modwsgi/.
18. ↑ "Pycnic Framework". http://pycnic.nullism.com.
19. ↑ "theintencity/restlite: Light-weight RESTful server tools in Python". https://github.com/theintencity/restlite.
20. ↑ "limodou/uliweb: Simple and easy use python web framework". https://github.com/limodou/uliweb.
21. ↑ "waitress documentation". https://docs.pylonsproject.org/projects/waitress/en/latest/.
22. ↑ "Welcome to". 2009-09-11. http://webpy.org/.
23. ↑ "weblayer — weblayer v0.4.3 documentation". http://packages.python.org/weblayer.
24. ↑ "Welcome | Werkzeug (The Python WSGI Utility Library)". http://werkzeug.pocoo.org/.
25. ↑ "CalDAV and CardDAV Server - A Simple Calendar and Contact Server". http://radicale.org/.
26. ↑ "Module ngx_http_uwsgi_module". http://nginx.org/en/docs/http/ngx_http_uwsgi_module.html.
27. ↑ "Configuration — NGINX Unit". https://unit.nginx.org/configuration/#python.
28. ↑ "Python Tools for Visual Studio - Documentation". https://pytools.codeplex.com/wikipage?title=wfastcgi.
29. ↑ "Google Code Archive - Long-term storage for Google Code Project Hosting". http://code.google.com/p/isapi-wsgi/.
30. ↑ "Python ISAPI Extension for IIS download | SourceForge.net". 2012-04-24. http://pyisapie.sourceforge.net/.

## External links

* PEP 333 – Python Web Server Gateway Interface
* PEP 3333 – Python Web Server Gateway Interface v1.0.1
* WSGI metaframework
* Comprehensive wiki about everything WSGI
* WSGI Tutorial
* Python standard library module wsgiref
* Getting Started with WSGI
* NWSGI – .NET implementation of the Python WSGI specification for IronPython and IIS
* Gevent-FastCGI server implemented using gevent coroutine-based networking library

* v
* t
* e

Python

Implementations|

* CircuitPython
* CLPython
* CPython
* Cython
* MicroPython
* Numba
* IronPython
* Jython
* Psyco
* PyPy
* Python for S60
* Shed Skin
* Stackless Python
* Unladen Swallow
* more...

IDE|

* Boa
* Eric Python IDE
* IDLE
* PyCharm
* PyDev
* SPE
* Ninja-IDE
* more...

Topics|

* WSGI

* software (list)
* Python Software Foundation
* PyCon

* v
* t
* e

Web interfaces

Server-side|

| Protocols|

* HTTP
* CGI
* SCGI
* FCGI
* AJP
* WSRP
* WebSocket

|

Server APIs|

* C NSAPI
* C ASAPI
* C ISAPI
* COM ASP
* Java servlet
* container
* CLI OWIN
* ASP.NET Handler
* Python WSGI
* Ruby Rack
* JavaScript JSGI
* Perl PSGI
* Lua WSAPI
* Portlet
* container

Apache modules|

* mod include
* mod jk
* mod lisp
* mod mono
* mod parrot
* mod perl
* mod php
* mod proxy
* mod python
* mod wsgi
* mod ruby
* Phusion Passenger

Topics|

* Web resource vs. Web service
* Open API
* Webhook
* Application server
* comparison
* Scripting

Client-side|

| Browser APIs|

* C NPAPI
* LiveConnect
* XPConnect
* C NPRuntime
* C PPAPI
* NaCl
* ActiveX
* BHO
* XBAP

|

Web APIs|

| W3C|

* Audio
* Canvas
* CORS
* DOM
* DOM events
* EME
* File
* Geolocation
* IndexedDB
* MSE
* SSE
* SVG
* Video
* WebAuthn
* WebRTC
* WebSocket
* Web messaging
* Web storage
* Web worker
* XMLHttpRequest
* WebAssembly

|

Khronos|

* WebCL
* WebGL

Others|

* Gears
* Web SQL Database (formerly W3C)
* WebUSB

Topics|

* Ajax and Remote scripting vs. DHTML
* Mashup
* Web IDL
* Scripting

Topics|

* Dynamic web page
* Web standards
* Rich web application
* Web API security
* Web application
* Web framework

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Web Server Gateway Interface. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Web_Server_Gateway_Interface&oldid=3011671"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template