Web Server Gateway Interface (WSGI)

来源:互联网 发布:手机桌面控制软件 编辑:程序博客网 时间:2024/06/18 11:43

The Web Server Gateway Interface (WSGI) is a specification for simple and universal interface between web servers and web applications or frameworks for the Python programming language.

注: WSGI是为 python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口

It was originally specified in PEP 333 authored by Phillip J. Ebay, and published on 7 December 2003. It has since been adopted as a standard for Python web application development. The latest version of the specification is v1.0.1, also known as PEP 3333, published on 26 September.


Background

Pyhton web frameworks had been a problem for new Python users since the choice of web framework would limit the choice of usable web servers, and vice versa. Python applications were often designed for only one of CGI, FastCGI, mod_python, or some other custom API of a specific web server.

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.


Specification overview

The WSGI has two sides: the “server” or “gateway” side (often a web server such as Apache or Nginx), and the “application” or “framework” side (the Python scipt itself).

To process a WSGI request, the server side executes the application and provides environment information and a callback function to the application side. The application processes the request, returning the response to the server side using the callback function it was provided.

注:A callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time.

Between the server and the application, there may be a WSGI middleware, which implements both sides of the API. The server receives a request from a client and forwards it to the middleware. After processing, it sends a request to the application. The application’s response is forwarded by the middleware to the server and ultimately to the client. There may be multiple middlewares forming a stack of WSGI-compliant applications.

注:WSGI 中间件实现了API的双方。对服务器来说,中间件扮演应用程序;对应用程序来说, 中间件扮演服务器。

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.
  • Performing content post-processing, such as applying XSLT stylesheets.
原创粉丝点击