Jump to content

Web Server Gateway Interface

From Wikipedia, the free encyclopedia

TheWeb Server Gateway Interface(WSGI,pronouncedwhiskey[1][2]orWIZ-ghee[3]) is a simplecalling conventionforweb serversto forward requests toweb applicationsorframeworkswritten 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.

Background[edit]

In 2003, Pythonweb frameworkswere typically written against onlyCGI,FastCGI,mod_ Python,or some other customAPIof 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-neutralinterfacebetween web servers and web applications or frameworks to promote common ground forportableweb application development.[4]

Specification overview[edit]

The WSGI has two sides:

  • theserver/gateway side. This is often running full web server software such asApacheorNginx,or is a lightweight application server that can communicate with a webserver, such asflup.
  • 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 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.

WSGI middleware[edit]

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 targetURL,after changing theenvironment variablesaccordingly.
  • Allowing multiple applications or frameworks to run side-by-side in the sameprocess
  • Load balancingand remote processing, by forwarding requests and responses over anetwork
  • Performing content post-processing, such as applyingXSLTstylesheets

Examples[edit]

Example application[edit]

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:

  • Line 1 defines a function[8]namedapplication,which takes two parameters,environandstart_response.environis a dictionary containingCGI environment variablesas well as other request parameters and metadata under well-defined keys.[9]start_responseis a callable itself, taking two positional parameters,statusandresponse_headers.
  • Line 2 callsstart_response,specifying "200 OK" as the HTTP status and a "Content-Type" response header.
  • Line 3 makes the function into agenerator.The body of the response is returned as an iterable ofbyte strings.

Example of calling an application[edit]

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 theenvirondict have been omitted.

fromioimportBytesIO

defcall_application(app,environ):
status=None
headers=None
body=BytesIO()

defstart_response(rstatus,rheaders):
nonlocalstatus,headers
status,headers=rstatus,rheaders

app_iter=app(environ,start_response)
try:
fordatainapp_iter:
assertstatusisnotNoneandheadersisnotNone,\
"start_response() was not called"
body.write(data)
finally:
ifhasattr(app_iter,'close'):
app_iter.close()
returnstatus,headers,body.getvalue()

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

WSGI-compatible applications and frameworks[edit]

Numerousweb frameworkssupport WSGI:

Currently wrappers are available forFastCGI,CGI,SCGI,AJP(using flup),twisted.web,Apache (usingmod_wsgiormod_ Python),Nginx(using ngx_http_uwsgi_module),[26]Nginx Unit(using the Python language module),[27]andMicrosoft IIS(using WFastCGI,[28]isapi-wsgi,[29]PyISAPIe,[30]or anASPgateway).

See also[edit]

References[edit]

  1. ^Simionato, Michele (June 11, 2007)."An Introduction to Web Programming with WSGI".
  2. ^Edge, Jake (July 9, 2019)."Mucking about with microframeworks".LWN.
  3. ^Goldberg, Kevin (2016-05-09)."An Introduction to Python WSGI Servers for Performance | AppDynamics".Application Performance Monitoring Blog | AppDynamics.Retrieved2020-08-20.
  4. ^ab"PEP 3333 - Python Web Server Gateway Interface v1.0.1".Python.org.Retrieved2018-04-04.
  5. ^"PEP 333 -- Python Web Server Gateway Interface v1.0".Python.org.Retrieved2018-04-04.
  6. ^"FrontPage - Python Wiki".Python.org.Retrieved2017-01-27.
  7. ^ab"PEP 3333 -- Python Web Server Gateway Interface v1.0.1".Python.org.Retrieved2018-04-04.
  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".Python.org.Retrieved2018-04-04.
  10. ^"Creating WSGI Middleware - Alan Christopher Thomas - Minted - PythonKC".YouTube.2015-08-28.Archivedfrom the original on 2021-12-12.Retrieved2017-01-27.
  11. ^"プエラリアジェル の hiệu quả は?".Bobo.digicool.Retrieved2017-01-27.
  12. ^"Django without mod_ Python, and WSGI support | Weblog | Django".Djangoproject.2005-07-18.Retrieved2017-01-27.
  13. ^"wsgi – WSGI server — Eventlet 0.20.1 documentation".Eventlet.net.Retrieved2017-01-27.
  14. ^"Falcon - Bare-metal web API framework for Python".Retrieved2017-10-22.
  15. ^"gevent-fastcgi 1.0.2.1: Python Package Index".Pypi. Python.org.2015-12-06.Retrieved2017-01-27.
  16. ^"anomaly/prestans: A WSGI compliant REST micro-framework".GitHub.Retrieved2017-01-27.
  17. ^"Google Code Archive - Long-term storage for Google Code Project Hosting".Code.google.Retrieved2017-01-27.
  18. ^"Pycnic Framework".Pycnic.nullism.Retrieved2017-01-27.
  19. ^"theintencity/restlite: Light-weight RESTful server tools in Python".GitHub.Retrieved2017-01-27.
  20. ^"limodou/uliweb: Simple and easy use Python web framework".GitHub.Retrieved2017-01-27.
  21. ^"waitress documentation".docs.pylonsproject.org.Retrieved2018-09-26.
  22. ^"Welcome to".Web.py.2009-09-11.Retrieved2017-01-27.
  23. ^"weblayer — weblayer v0.4.3 documentation".Packages. Python.org.Retrieved2017-01-27.
  24. ^"Welcome | Werkzeug (The Python WSGI Utility Library)".Werkzeug.pocoo.org.Retrieved2017-01-27.
  25. ^"CalDAV and CardDAV Server - A Simple Calendar and Contact Server".Radicale.org.Retrieved2017-01-27.
  26. ^"Module ngx_http_uwsgi_module".Nginx.org.Retrieved2017-01-27.
  27. ^"Configuration — NGINX Unit".Unit.nginx.org.Retrieved2023-05-04.
  28. ^"Python Tools for Visual Studio - Documentation".Pytools.codeplex.Retrieved2017-01-27.
  29. ^"Google Code Archive - Long-term storage for Google Code Project Hosting".Code.google.Retrieved2017-01-27.
  30. ^"Python ISAPI Extension for IIS download | SourceForge.net".Pyisapie.sourceforge.net.2012-04-24.Retrieved2017-01-27.

External links[edit]