An Introduction to the Python Web Server Gateway Interface

WSGI(Web服务器网关接口)是一个PEP333规范,为Web服务器和Python Web框架/应用程序提供了一个标准化的接口。其目标是创建一个简单而全面的接口,支持所有(或大多数)Web服务器与Web框架之间的交互。通过WSGI,开发者可以使用中间件组件进行请求预处理和后处理,如压缩、记录、代理、负载均衡等。
非原创,来源互联网


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.
Python交互式编程入门》是一门介绍如何使用Python进行交互式编程的课程。在这门课程中,学习者将学习如何使用Python编写简单的程序并与之进行交互。 首先,课程将介绍Python的基本语法和编程概念。学习者将学习如何定义变量、使用条件语句和循环、编写函数以及处理列表和字典等数据结构。 接下来,课程将介绍Python交互式编程的概念。学习者将学习如何使用Python的交互式命令行界面进行编程,并了解如何与Python解释器进行交互。他们将学习如何编写单行和多行的Python代码,并立即查看结果。 在这门课程中,学习者还将学习如何使用Python编写简单的图形用户界面(GUI)应用程序。他们将学习如何使用Python的GUI库来创建窗口、按钮、文本框等界面元素,并学习如何为这些元素添加交互功能。 此外,课程还将涵盖一些常见的Python编程任务,如文件操作、网络编程和数据可视化。通过这些任务的实践,学习者将能够更好地理解Python交互式编程的应用。 该课程注重实践,学习者将通过编写小型项目和解决编程问题来应用所学知识。他们将有机会与其他学习者一起合作,分享代码和解决方案,并从其他人的经验中学习。 总之,《Python交互式编程入门》是一门适合初学者的课程,它将帮助学习者掌握Python编程的基本概念和技能,并提供实践经验和项目练习。通过这门课程的学习,学习者将能够熟练地运用Python进行交互式编程,并在日后的编程任务中受益。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值