非原创,来源互联网
In Brief
WSGI is a specification, laid out in PEP 333, for a standardized interface between Web servers and Python Web frameworks/applications.
The goal is to provide a relatively simple yet comprehensive interface capable of supporting all (or most) interactions between a Web server and a Web framework. (Think "CGI" but programmatic rather than I/O based.)
An additional goal is to support "middleware" components for pre- and post-processing of requests: think gzip, recording, proxy, load-balancing.
No mechanism for deployment and relatively few mechanisms for configuration are specified in the PEP. (That's a problem Python Paste targets.)
All you really need to know...
Unless you are developing a new Web app framework (admittedly, p > 0.5...) you don't care about WSGI. Use a Web app framework. They all support WSGI at this point, which means you basically need to worry about configuration and deployment of your app, and nothing else.
Summary of Specification
The PEP specifies three roles: the role of a server, the role of a framework/app, and the role of a middleware object.
Web server side
The server must provide two things: an environ dictionary, and a start_response function. The environ dictionary needs to have the usual things present -- it's similar to the CGI environment. start_response is a callable that takes two arguments, status -- containing a standard HTTP status string like 200 OK -- and response_headers -- a list of standard HTTP response headers.
The Web server dispatches a request to the framework/app by calling the application:
iterable = app(environ, start_response)
for data in iterable:
# send data to client
It's the framework/app's responsibility to build the headers, call start_response, and build the data returned in iterable. It's the Web server's responsibility to serve both the headers and the data up via HTTP.
Web framework/app side
The Web framework/app is represented to the server as a Python callable. It can be a class, an object, or a function. The arguments to __init__, __call__, or the function must be as above: an environ object and a start_response callable.
The Web framework/app must call start_response before returning or yielding any data.
The Web framework/app should return any data in an iterable form -- e.g. return [ page ].
Middleware
Middleware components must obey both the Web server side and the Web app/framework side of things, plus a few more minor niggling restrictions. Middleware should be as transparent as possible.
An example WSGI application
Here's a very simple WSGI application that returns a static "Hello world!" page.
def simple_app(environ, start_response):
status = '200 OK'
response_headers = [('Content-type','text/plain')]
start_response(status, response_headers)
return ['Hello world!\n']
Here's a very simple middleware application that uppercases everything sent:
class Upperware:
def __init__(self, app):
self.wrapped_app = app
def __call__(self, environ, start_response):
for data in self.wrapped_app(environ, start_response):
return data.upper()
To instantiate/run these from the server's perspective, you just do the obvious:
serve(simple_app)
or
wrapped_app = Upperware(simple_app)
serve(wrapped_app)
Yes, it can be that simple: here's my code for running Trac via scgiserver.
#!/usr/bin/env python
from trac.web.main import dispatch_request as app
from scgiserver import serve_application
PREFIX=''
PORT=4101
serve_application(app, PREFIX, PORT)
Omitted complexity
start_response must also return a write_fn callable that legacy apps can use as a file-like stream. This makes it easy to convert old apps to be "WSGI compliant". Its use is deprecated because using a file-like stream puts the reins of execution in the hands of the app object, preventing intelligent content handling interleaving/asynchrony on the part of the server.
start_response can take an optional parameter, exc_info, that is used for error handling when present.
分享到:
相关推荐
The purpose of this book is to show you how to write Python programs in good idiomatic Python 3 style, and to be a useful reference for the Python 3 language after the initial reading. Although Python...
This textbook provides an introduction to the free software Python and its use for statistical data analysis. It covers common statistical tests for continuous, discrete and categorical data, as well ...
An Introduction to the Bootstrap_Efron,an excellent book, and worth a reading by most students and practitioners in statistics... Throughout the book, the authors have spent a lot of effort in ...
本书《An Introduction to Python》作为Python初学者的入门教材,对Python编程语言进行了基础介绍。虽然书中内容简单易懂,但对于初学者来说,它提供了足够的信息来理解Python的核心概念。 知识点一:Python基础 ...
An Introduction to the Event-Related Potential Technique.PDF 是一个STEVEN J。LUCK的经典ERP实验指导书,2009年由范思陆翻译成中文,华东师范大学出版《事件相关电位基础》
根据提供的文件信息,本文将介绍金融衍生品的数学基础,这些内容来源于《An Introduction to the Mathematics of Financial Derivatives》这本书的3rd Edition版本。Ali Hirsa是该书的编辑者,而Salih N. Neftci则是...
An introduction to Python & Algorithms Marina von Steinkirch (bt3) @ b t 3 Summer, 2013 \There's nothing to fear but the fear itself. That's called recursion, and that would lead you to innite fear.
An Introduction to Statistics with Python With Applications in the Life Sciences by Thomas Haslwanter (z-lib.org)
An Introduction to the Analysis of Algorithms(2nd) 英文无水印pdf 第2版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自...
#### 标题:“An introduction to the bootstrap” #### 描述:“An introduction to the bootstrap,介绍bootstrap很好的一本牛书!” #### 解析: - **Bootstrap**是一种统计学中的重抽样技术,它通过从原始数据...
### 编程在Python 3:Python语言的完整介绍(第二版) #### 书籍概述 《编程在Python 3:Python语言的完整介绍》(第二版)由马克·萨默菲尔德编写,是一本全面介绍Python 3语言的书籍。本书不仅适合Python初学者...
An Introduction to Python and Computer Programming 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,...
向读者非正式地介绍Python语言和系统的基本概念和功能。 可以方便地使用Python解释器以获得实际经验,但是所有示例都是独立的,因此该教程也可以脱机阅读。
Perkovic's Introduction to Programming Using Python provides an imperative-first introduction to Python focusing on computer applications and the process of developing them. The text helps develop ...
The all-inclusive treatise on "Astrodynamics" by Richard H. Battin clearly must be counted among the great classics in scientific literature. This text documents the fundamental theoretical ...
### 实用编程:计算机科学导论使用Python(2009年版) #### 知识点一:Python作为教学语言的选择 《实用编程》一书选择Python作为介绍计算机科学概念的语言,这反映了Python在教育领域的广泛应用。Python因其简洁...
An introduction to the Analysis of Algorithms, Robert Sedgewick 著
经典书籍:An Introduction to the Theory of Graph Spectra PDF版