TheWeb Server Gateway Interface (WSGI, pronouncedwhiskey[1][2] orWIZ-ghee[3]) is a simplecalling convention forweb servers to forward requests toweb applications orframeworks written in thePython programming language. The current version of WSGI, version 1.0.1, is specified inPython Enhancement Proposal (PEP) 3333.[4]
WSGI was originally specified as PEP-333 in 2003.[5] PEP-3333, published in 2010, updates the specification forPython 3.
In 2003, Pythonweb frameworks were typically written against onlyCGI,FastCGI,mod_python, or some other customAPI of a specificweb 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-neutralinterface between web servers and web applications or frameworks to promote common ground forportable web application development.[4]
The WSGI has two sides:
Between the server and the application, there may be one or moreWSGImiddleware 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.
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]
A WSGI-compatible "Hello, World!" application written inPython:
defapplication(environ,start_response):start_response("200 OK",[("Content-Type","text/plain")])yieldb"Hello, World!\n"
Where:
application, which takes two parameters,environ andstart_response.environ is a dictionary containingCGI 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 andresponse_headers.start_response, specifying "200 OK" as the HTTP status and a "Content-Type" response header.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 theenviron dict have been omitted.
fromioimportBytesIOdefcall_application(app,environ):status=Noneheaders=Nonebody=BytesIO()defstart_response(rstatus,rheaders):nonlocalstatus,headersstatus,headers=rstatus,rheadersapp_iter=app(environ,start_response)try:fordatainapp_iter:assert(statusisnotNoneandheadersisnotNone),"start_response() was not called"body.write(data)finally:ifhasattr(app_iter,"close"):app_iter.close()returnstatus,headers,body.getvalue()environ={...}# "environ" dictstatus,headers,body=call_application(app,environ)
This sectionmay containexcessive orirrelevant examples. Please helpimprove it by removingless pertinent examples andelaborating on existing ones.(September 2018) (Learn how and when to remove this message) |
Numerousweb frameworks support WSGI:
Currently wrappers are available forFastCGI,CGI,SCGI,AJP (using flup),twisted.web, Apache (usingmod_wsgi ormod_python),Nginx (using ngx_http_uwsgi_module),[27]Nginx Unit (using the Python language module),[28] andMicrosoft IIS (using WFastCGI,[29] isapi-wsgi,[30] PyISAPIe,[31] or anASP gateway).
__call__ method"