Web Server Gateway Interface

This is an old revision of this page, as edited by 84.61.145.57 (talk) at 14:37, 2 September 2007. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

The Web Server Gateway Interface defines a simple and universal interface between web servers and web applications or frameworks for the Python programming language.

Idea

Python boasts a wide variety of web application frameworks. This 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.

WSGI proposes a simple and universal interface between web servers and web applications or frameworks.

Specification overview

The WSGI interface has two sides: the "server" or "gateway" side, and the "application" or "framework" side. The server side invokes a callable object (usually a function or a method) that is provided by the application side. Additionally WSGI provides middlewares; a 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 XSL stylesheets

Example application

A WSGI compatible "Hello World" application:

def app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return ['Hello World\n']

WSGI-compatible applications and frameworks

There are numerous Web application frameworks supporting WSGI:

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), mod_python as well as Microsoft IIS (using an ASP gateway).

mod_wsgi [2], an Apache module which can directly load applications is also in work.