WSGI
Encyclopedia
The Web Server Gateway Interface (WSGI) (sometimes pronounced as 'wiz-gee' or 'whiskey') defines a simple and universal interface
between web server
s and web application
s or framework
s for the Python programming language
.
s, and vice versa. Python applications were often designed for either CGI
, FastCGI
, mod python
or even custom API interfaces of specific web-servers.
WSGI was created as a low-level interface
between web servers and web applications or frameworks to promote common ground for portable web application development.
” or “gateway” side, and the “application” or “framework” side. The server side calls the application side, providing environment information plus a callback function (for the application to use to convey headers to the server side), and receiving web content in return.
So-called WSGI middleware
implements both sides of the API so that it can intermediate between a WSGI server and a WSGI application: the middleware acts as an application from some WSGI server's point of view and as a server from some WSGI application's point of view. A “middleware” component can perform such functions as:
:
Where:
s supporting WSGI:
Currently wrappers are available for FastCGI
, CGI
, SCGI, AJP
(using flup), twisted.web
, Apache (using mod wsgi
or mod python
) and Microsoft IIS
(using isapi-wsgi, PyISAPIe, or an ASP
gateway).
A reworked WSGI spec Web3 has also been proposed, specified in PEP444. This standard is an incompatible derivative of WSGI designed to work on Python 2.6, 2.7, 3.1+.
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...
between web server
Web server
Web server can refer to either the hardware or the software that helps to deliver content that can be accessed through the Internet....
s and web application
Web application
A web application is an application that is accessed over a network such as the Internet or an intranet. The term may also mean a computer software application that is coded in a browser-supported language and reliant on a common web browser to render the application executable.Web applications are...
s or framework
Web application framework
A web application framework is a software framework that is designed to support the development of dynamic websites, web applications and web services. The framework aims to alleviate the overhead associated with common activities performed in Web development...
s for the Python programming language
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
.
Idea
Historically, Python web application frameworks have been a problem for new Python users because, generally speaking, the choice of web framework would limit the choice of usable web serverWeb server
Web server can refer to either the hardware or the software that helps to deliver content that can be accessed through the Internet....
s, and vice versa. Python applications were often designed for either CGI
Common Gateway Interface
The Common Gateway Interface is a standard method for web servers software to delegate the generation of web pages to executable files...
, FastCGI
FastCGI
FastCGI is a protocol for interfacing interactive programs with a web server. FastCGI is a variation on the earlier Common Gateway Interface ; FastCGI's main aim is to reduce the overhead associated with interfacing the web server and CGI programs, allowing a server to handle more web page...
, mod python
Mod python
mod_python is an Apache HTTP Server module that integrates the Python programming language into the Apache server. It is intended to replace Common Gateway Interface as a method of executing Python scripts on a web server. The promised benefits are faster execution speed and maintaining data over...
or even custom API interfaces of specific web-servers.
WSGI was created as a low-level interface
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...
between web servers and web applications or frameworks to promote common ground for portable web application development.
Specification overview
The WSGI has two sides: the “serverServer (computing)
In the context of client-server architecture, a server is a computer program running to serve the requests of other programs, the "clients". Thus, the "server" performs some computational task on behalf of "clients"...
” or “gateway” side, and the “application” or “framework” side. The server side calls the application side, providing environment information plus a callback function (for the application to use to convey headers to the server side), and receiving web content in return.
So-called WSGI middleware
Middleware
Middleware is computer software that connects software components or people and their applications. The software consists of a set of services that allows multiple processes running on one or more machines to interact...
implements both sides of the API so that it can intermediate between a WSGI server and a WSGI application: the middleware acts as an application from some WSGI server's point of view and as a server from some WSGI application's point of view. A “middleware” component can perform such functions as:
- Routing a request to different application objects based on the target URLUniform Resource LocatorIn computing, a uniform resource locator or universal resource locator is a specific character string that constitutes a reference to an Internet resource....
, after changing the environment variables accordingly. - Allowing multiple applications or frameworks to run side-by-side in the same processProcess (computing)In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system , a process may be made up of multiple threads of execution that execute instructions concurrently.A computer program is a...
- Load balancingLoad balancing (computing)Load balancing is a computer networking methodology to distribute workload across multiple computers or a computer cluster, network links, central processing units, disk drives, or other resources, to achieve optimal resource utilization, maximize throughput, minimize response time, and avoid...
and remote processing, by forwarding requests and responses over a networkComputer networkA computer network, often simply referred to as a network, is a collection of hardware components and computers interconnected by communication channels that allow sharing of resources and information.... - Perform content postprocessing, such as applying XSLTXSLTXSLT is a declarative, XML-based language used for the transformation of XML documents. The original document is not changed; rather, a new document is created based on the content of an existing one. The new document may be serialized by the processor in standard XML syntax or in another format,...
stylesheets
Example application
A WSGI-compatible “Hello World” application written in PythonPython (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
:
Where:
- Line 1 defines a callable named application, which takes two parameters, and . is a dictionary containing environment variables in CGI. is a callable taking two required parameters and .
- Line 2 calls , specifying “200 OK” as the status and a “Content-Type” header.
- Line 3 returns the body of response as a string literal.
Example of calling an application
An example of calling an application and retrieving its response:WSGI-compatible applications and frameworks
There are numerous web application frameworkWeb application framework
A web application framework is a software framework that is designed to support the development of dynamic websites, web applications and web services. The framework aims to alleviate the overhead associated with common activities performed in Web development...
s supporting WSGI:
- CherryPyCherryPyCherryPy 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 .CherryPy can be a web server...
- Django
- web.py
- web2pyWeb2pyWeb2py is an open source web application framework. Web2py is written in the Python language and is programmable in Python. Since web2py was originally designed as a teaching tool with emphasis on ease of use and deployment, it does not have any project-level configuration files. Web2py was...
- TurboGearsTurboGearsTurboGears is a Python web application framework consisting of several WSGI components such as Pylons, SQLAlchemy, Genshi and Repoze.TurboGears is designed around the model-view-controller architecture, much like Struts or Ruby on Rails, designed to make rapid web application development in Python...
- TornadoTornado (web server)Tornado is a scalable, non-blocking web server and web application framework. It was developed for use by FriendFeed; the company was acquired by Facebook in 2009 and Tornado was open-sourced soon after.- Performance :...
- Pylons
- BlueBream
- Google App EngineGoogle App EngineGoogle App Engine is a platform as a service cloud computing platform for developing and hosting web applications in Google-managed data centers. It virtualizes applications across multiple servers,...
- TracTracTrac is an open source, web-based project management and bug-tracking tool. The program is inspired by CVSTrac, and was originally named svntrac due to its ability to interface with Subversion. It is developed and maintained by Edgewall Software....
- FlaskFlask (programming)Flask is a lightweight web application framework written in Python and based on the Werkzeug WSGI toolkit and Jinja2 template engine. It is BSD licensed....
- Pyramid
- Bottle
- weblayer
Wrappers
The server or gateway invokes the application callable once for each request it receives from an HTTP client, that is directed at the application.Currently wrappers are available for FastCGI
FastCGI
FastCGI is a protocol for interfacing interactive programs with a web server. FastCGI is a variation on the earlier Common Gateway Interface ; FastCGI's main aim is to reduce the overhead associated with interfacing the web server and CGI programs, allowing a server to handle more web page...
, CGI
Common Gateway Interface
The Common Gateway Interface is a standard method for web servers software to delegate the generation of web pages to executable files...
, SCGI, AJP
Apache JServ Protocol
The Apache JServ Protocol is a binary protocol that can proxy inbound requests from a web server through to an application server that sits behind the web server. It also supports some monitoring in that the web server can ping the application server...
(using flup), twisted.web
Twisted (software)
Twisted is an event-driven network programming framework written in Python and licensed under the MIT License.Twisted projects variously support TCP, UDP, SSL/TLS, IP Multicast, Unix domain sockets, a large number of protocols , and much more...
, Apache (using mod wsgi
Mod wsgi
mod_wsgi is an Apache HTTP Server module by Graham Dumpleton that provides a WSGI compliant interface for hosting Python 2.3+ based web applications under Apache. As of version 3.0, mod_wsgi supports Python 2.6 and 3.1...
or mod python
Mod python
mod_python is an Apache HTTP Server module that integrates the Python programming language into the Apache server. It is intended to replace Common Gateway Interface as a method of executing Python scripts on a web server. The promised benefits are faster execution speed and maintaining data over...
) and Microsoft IIS
Internet Information Services
Internet Information Services – formerly called Internet Information Server – is a web server application and set of feature extension modules created by Microsoft for use with Microsoft Windows. It is the most used web server after Apache HTTP Server. IIS 7.5 supports HTTP, HTTPS,...
(using isapi-wsgi, PyISAPIe, or an ASP
Active Server Pages
Active Server Pages , also known as Classic ASP or ASP Classic, was Microsoft's first server-side script engine for dynamically-generated Web pages. Initially released as an add-on to Internet Information Services via the Windows NT 4.0 Option Pack Active Server Pages (ASP), also known as Classic...
gateway).
WSGI and Python 3
The separation of binary and text data in Python 3 poses a problem for WSGI, as it specifies that header data should be strings, while it sometimes needs to be binary and sometimes text. This works in Python 2 where text and binary data both are kept in "string" variables, but in Python 3 binary data is kept in "bytes" variables and "string" variables are for unicode text data. An updated version of the WSGI specification that deals with this is PEP 3333.A reworked WSGI spec Web3 has also been proposed, specified in PEP444. This standard is an incompatible derivative of WSGI designed to work on Python 2.6, 2.7, 3.1+.
See also
- RackRack (web server interface)Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between into a single method call.Rack is...
: RubyRuby (programming language)Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
web server interface - PSGIPSGIPSGI or Perl Web Server Gateway Interface is an interface between Web servers and Perl-based Web applications and frameworks that allows writing portable applications that can be run as standalone servers or using CGI, FastCGI, mod_perl, et al...
: PerlPerlPerl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
Web Server Gateway Interface - SCGISimple Common Gateway InterfaceThe Simple Common Gateway Interface is a protocol for applications to interface with HTTP servers, as an alternative to the CGI protocol...