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] 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

Related Research Articles

<span class="mw-page-title-main">Common Gateway Interface</span> Interface between Web servers and external programs

In computing, Common Gateway Interface (CGI) is an interface specification that enables web servers to execute an external program to process HTTP or HTTPS 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.

In computing, a solution stack or software stack is a set of software subsystems or components needed to create a complete platform such that no additional software is needed to support applications. Applications are said to "run on" or "run on top of" the resulting platform.

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 runs on a web server. It 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.

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

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

Nginx is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. The software was created by Russian developer Igor Sysoev and publicly released in 2004. Nginx is free and open-source software, released under the terms of the 2-clause BSD license. A large fraction of web servers use Nginx, often as a load balancer.

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".

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.

Phusion Passenger is a free web server and application server with support for Ruby, Python and Node.js. It is designed to integrate into the Apache HTTP Server or the nginx web server, but also has a mode for running standalone without an external web server. Phusion Passenger supports Unix-like operating systems, and is available as a gem package, as a tarball, or as native Linux packages.

<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.

JSGI, or JavaScript Gateway Interface, is an interface between web servers and JavaScript-based web applications and frameworks. It was inspired by the Rack for Ruby and WSGI for Python and was one of the inspirations of PSGI for Perl.

<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.

FastCGI is a binary protocol for interfacing interactive programs with a web server. It is a variation on the earlier Common Gateway Interface (CGI). FastCGI's main aim is to reduce the overhead related to interfacing between web server and CGI programs, allowing a server to handle more web page requests per unit of time.

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

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.

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 the 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 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. "Configuration — NGINX Unit". Unit.nginx.org. Retrieved 2023-05-04.
  28. "Python Tools for Visual Studio - Documentation". Pytools.codeplex.com. Retrieved 2017-01-27.
  29. "Google Code Archive - Long-term storage for Google Code Project Hosting". Code.google.com. Retrieved 2017-01-27.
  30. "Python ISAPI Extension for IIS download | SourceForge.net". Pyisapie.sourceforge.net. 2012-04-24. Retrieved 2017-01-27.