Web Server Gateway Interface
From Wikipedia, the free encyclopedia
The Web Server Gateway Interface defines a simple and universal interface between web servers and web applications or frameworks for the Python programming language. The latest version 3.0 of Python, released in December 2008, is already supported by mod_wsgi (a module for the Apache Web server).
Contents |
[edit] 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 servers, and vice versa. Python applications were often designed for either CGI, FastCGI, mod_python or even custom API interfaces of specific web-servers.
WSGI[1] (sometimes pronounced 'whiskey' or 'wiz-gee') was created as a low-level interface between web servers and web applications or frameworks to promote common ground for portable web application development. WSGI is based on the existing CGI standard.
[edit] Specification overview
The WSGI has two sides: the "server" or "gateway" side, and the "application" or "framework" side. The server side invokes[clarification needed] a callable object (usually a function or a method) that is provided by the application side. Additionally WSGI provides middleware; WSGI middleware implements both sides of the API, so that it can be inserted "between" a WSGI server and a WSGI application -- the middleware will act as an application from the server's point of view, and as a server from the application's point of view.
A "middleware" component can perform such functions as:
- Routing a request to different application objects based on the target URL, after changing the environment variables accordingly.
- Allowing multiple applications or frameworks to run side-by-side in the same process
- Load balancing and remote processing, by forwarding requests and responses over a network
- Perform content postprocessing, such as applying XSLT stylesheets
[edit] Example application
A WSGI compatible "Hello World" application in Python syntax:
def app(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) return ['Hello World\n']
[edit] Example of calling an application
An example of calling an application and retrieving its response:
def call_application(app, environ): body = [] status_headers = [None, None] def start_response(status, headers): status_headers[:] = [status, headers] return body.append app_iter = app(environ, start_response) try: for item in app_iter: body.append(item) finally: if hasattr(app_iter, 'close'): app_iter.close() return status_headers[0], status_headers[1], ''.join(body) status, headers, body = call_application(app, {...environ...})
[edit] WSGI-compatible applications and frameworks
There are numerous Web application frameworks supporting WSGI:
- CherryPy
- Django[2]
- TurboGears
- PyAMF
- Pylons
- web.py (webpy.org)
- Zope 3
- Google App Engine
- web2py
- Werkzeug (werkzeug.pocoo.org, Tool "werkzeug" on pocoo.org)
[edit] 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, CGI, SCGI, AJP (using flup), Apache (using mod_wsgi or mod_python) and Microsoft IIS (using isapi-wsgi, PyISAPIe, or an ASP gateway).
[edit] References
[edit] External links
- WSGI metaframework
- Comprehensive wiki about everything WSGI
- WSGI Tutorial
- Python standard library module wsgiref
|