Web Server Gateway Interface

Last updated

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]

Contents

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:

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]

Examples

Example application

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

defapplication(environ,start_response):start_response('200 OK',[('Content-Type','text/plain')])yieldb'Hello, World!\n'

Where:

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.

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:assertstatusisnotNoneandheadersisnotNone, \                 "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)

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

See also

Related Research Articles

In computing, Common Gateway Interface (CGI) is an interface specification that enables web servers to execute an external program, typically to process user requests.

mod_python is an Apache HTTP Server module that integrates the Python programming language with the server. It is intended to provide a Python language binding for the Apache HTTP Server.

CherryPy is an object-oriented web application framework using the Python programming language. It is designed for rapid development of web applications by wrapping the HTTP protocol but stays at a low level and does not offer much more than what is defined in RFC 7231.

<span class="mw-page-title-main">Django (web framework)</span> Python web framework

Django is a free and open-source, Python-based web framework that follows the model–template–views (MTV) architectural pattern. It is maintained by the Django Software Foundation (DSF), an independent organization established in the US as a 501(c)(3) non-profit.

The Simple Common Gateway Interface (SCGI) is a protocol for applications to interface with HTTP servers, as an alternative to the CGI protocol. It is similar to FastCGI but is designed to be easier to parse. Unlike CGI, it permits a long-running service process to continue serving requests, thus avoiding delays in responding to requests due to setup overhead.

A web framework (WF) or web application framework (WAF) is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Web frameworks provide a standard way to build and deploy web applications on the World Wide Web. Web frameworks aim to automate the overhead associated with common activities performed in web development. For example, many web frameworks provide libraries for database access, templating frameworks, and session management, and they often promote code reuse. Although they often target development of dynamic web sites, they are also applicable to static websites.

<span class="mw-page-title-main">TurboGears</span>

TurboGears is a Python web application framework consisting of several WSGI components such as WebOb, SQLAlchemy, Genshi and Repoze.

Python Paste, often simply called paste, is a set of utilities for web development in Python. Paste has been described as "a framework for web frameworks".

mod_wsgi is an Apache HTTP Server module by Graham Dumpleton that provides a WSGI compliant interface for hosting Python based web applications under Apache. As of version 4.5.3, mod_wsgi supports Python 2 and 3.

Google App Engine is a cloud computing platform as a service for developing and hosting web applications in Google-managed data centers. Applications are sandboxed and run across multiple servers. App Engine offers automatic scaling for web applications—as the number of requests increases for an application, App Engine automatically allocates more resources for the web application to handle the additional demand.

Web2py is an open-source web application framework written in the Python programming language. Web2py allows web developers to program dynamic web content using Python. Web2py is designed to help reduce tedious web development tasks, such as developing web forms from scratch, although a web developer may build a form from scratch if required.

<span class="mw-page-title-main">Rack (web server interface)</span> API specification for web applications in programming language Ruby

Rack is a modular interface between web servers and web applications developed in the Ruby programming language. With Rack, application programming interfaces (APIs) for web frameworks and middleware are wrapped into a single method call handling HTTP requests and responses.

<span class="mw-page-title-main">Plack (software)</span>

Plack is a Perl web application programming framework inspired by Rack for Ruby and WSGI for Python, and it is the project behind the PSGI specification used by other frameworks such as Catalyst and Dancer. Plack allows for testing of Perl web applications without a live web server.

<span class="mw-page-title-main">Flask (web framework)</span> Python web framework

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools.

<span class="mw-page-title-main">Gunicorn</span>

The Gunicorn "Green Unicorn" is a Python Web Server Gateway Interface (WSGI) HTTP server. It is a pre-fork worker model, ported from Ruby's Unicorn project. The Gunicorn server is broadly compatible with a number of web frameworks, simply implemented, light on server resources and fairly fast. It is often paired with NGINX, as the two have complementary features.

PythonAnywhere is an online integrated development environment (IDE) and web hosting service based on the Python programming language. Founded by Giles Thomas and Robert Smithson in 2012, it provides in-browser access to server-based Python and Bash command-line interfaces, along with a code editor with syntax highlighting. Program files can be transferred to and from the service using the user's browser. Web applications hosted by the service can be written using any WSGI-based application framework.

uWSGI is an open source software application that "aims at developing a full stack for building hosting services". It is named after the Web Server Gateway Interface (WSGI), which was the first plugin supported by the project. uWSGI is maintained by Italian based software company unbit.

The Asynchronous Server Gateway Interface (ASGI) is a calling convention for web servers to forward requests to asynchronous-capable Python programming language frameworks, and applications. It is built as a successor to the Web Server Gateway Interface (WSGI).

References

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