详细说明
The application object must accept two positional arguments. For the sake of illustration, we have named them environ and start_response, but they are not required to have these names. A server or gateway must invoke the application object using positional (not keyword) arguments. (E.g. by calling result = application(environ,start_response) as shown above.)
The environ parameter is a dictionary object, containing CGI-style environment variables. This object must be a builtin Python dictionary (not a subclass, UserDict or other dictionary emulation), and the application is allowed to modify the dictionary in any way it desires. The dictionary must also include certain WSGI-required variables (described in a later section), and may also include server-specific extension variables, named according to a convention that will be described below.
应用程序对象必须接受两个位置参数.为了更好的解释,我们叫他们一个为environ一个为start_response( = res),但他们并不要求一定是这个名字.服务端必须使用位置参数(而不是关键字参数)来调用应用程序对象.
例如得调用 result = application(environ, start_response)
environ是一个字典, 包括了CGI环境变量.environ必须是 python内建的字典对象dict(不是其子类, 用户字典或其他模拟的字典), 应用程序可以以任何方式改变environ. environ必须包含来一些WSGI必需的变量(在下一章节说明),和服务器专有的变量, 按照惯例命名方式见下文.
The start_response parameter is a callable accepting two required positional arguments, and one optional argument. For the sake of illustration, we have named these arguments status, response_headers, and exc_info, but they are not required to have these names, and the application must invoke the start_response callable using positional arguments (e.g. start_response(status,response_headers)).
The status parameter is a status string of the form "999 Message here", and response_headers is a list of (header_name,header_value) tuples describing the HTTP response header. The optional exc_info parameter is described below in the sections on The start_response() Callable and Error Handling. It is used only when the application has trapped an error and is attempting to display an error message to the browser.
res参数是一个包含了两个位置参数,一个可选参数的可调用对象. 为了说明, 我们叫这三个参数分别为 status, response_headers 和 exc_info, 当然他们也不是必需使用这个名字.应用程序必须使用位置参数来调用他们.
status参数是一个类似"999 Message here"格式的状态字符串, reponse_headers是一个包含描述HTTP响应头的(header_name, header_value)元祖的列表, 可选参数exc_info 仅仅被使用在应用程序捕获到错误并试图把错误信息显示到浏览器的时候.
The start_response callable must return a write(body_data) callable that takes one positional parameter: a string to be written as part of the HTTP response body. (Note: the write() callable is provided only to support certain existing frameworks' imperative output APIs; it should not be used by new applications or frameworks if it can be avoided. See the Buffering and Streaming section for more details.)
When called by the server, the application object must return an iterable yielding zero or more strings. This can be accomplished in a variety of ways, such as by returning a list of strings, or by the application being a generator function that yields strings, or by the application being a class whose instances are iterable. Regardless of how it is accomplished, the application object must always return an iterable yielding zero or more strings.
res对象必须返回一个接受一个位参(一个写成HTTP响应体的字符串)的可写可调用对象.(注意: write()现在仅提供给一些已存在的框架来支持其需要的输出接口.在编写新的应用或框架时如果可以避免就不要使用它.更多细节可参考Buffering and Streaming 部分)
当服务端调用时, 应用程序对象必须返回一个可迭代的对象以生成零到多个字符串.这可以以多种方式实现,比如返回一个字符串列表,或者制作一个生成器函数来生成字符串(yield), 或者返回一个可迭代的实例.不论如何实现,应用程序对象必须返回一个可迭代输出零到多个字符串的迭代器.
The server or gateway must transmit the yielded strings to the client in an unbuffered fashion, completing the transmission of each string before requesting another one. (In other words, applications should perform their own buffering. See the Buffering and Streaming section below for more on how application output must be handled.)
The server or gateway should treat the yielded strings as binary byte sequences: in particular, it should ensure that line endings are not altered. The application is responsible for ensuring that the string(s) to be written are in a format suitable for the client. (The server or gateway may apply HTTP transfer encodings, or perform other transformations for the purpose of implementing HTTP features such as byte-range transmission. See Other HTTP Features, below, for more details.)
If a call to len(iterable) succeeds, the server must be able to rely on the result being accurate. That is, if the iterable returned by the application provides a working __len__() method, it must return an accurate result. (See the Handling the Content-Length Header section for information on how this would normally be used.)
服务端必须把生成的字符串以无缓冲的方式传递给客户端, 在另一个请求到来前完成每个字符串的传输.参见下面的Buffering and Streaming章节了解关于应用程序如何处理输出.
服务端应该将这些字符串视为二进制字节序列,尤其要确保行结束符没有改动.应用程序有责任确定字符串格式适用于客户端.(服务端可能会队HTTP传输编码,或者为了实现服务器特性执行其他转换例如字节范围内传输. 更多细节参见下面
Other HTTP Features章节.)
如果对于返回的迭代器求长(len)成功了,服务端应该相信这个结果是精确的.就是说,如果从应用程序返回的迭代器提供了__len__()方法, 它肯定会返回一个精确地结果.(参看如何处理Content-Length Header章节会知道要使用这个结果.)
If the iterable returned by the application has a close() method, the server or gateway must call that method upon completion of the current request, whether the request was completed normally, or terminated early due to an error. (This is to support resource release by the application. This protocol is intended to complement PEP 325's generator support, and other common iterables with close() methods.
(Note: the application must invoke the start_response() callable before the iterable yields its first body string, so that the server can send the headers before any body content. However, this invocation may be performed by the iterable's first iteration, so servers must not assume that start_response() has been called before they begin iterating over the iterable.)
Finally, servers and gateways must not directly use any other attributes of the iterable returned by the application, unless it is an instance of a type specific to that server or gateway, such as a "file wrapper" returned by wsgi.file_wrapper (see Optional Platform-Specific File Handling). In the general case, only attributes specified here, or accessed via e.g. the PEP 234 iteration APIs are acceptable.
如果迭代器还提供了close()方法,服务端必须在当前请求完成时调用该方法,无论该请求是正常的结束了还是由于错误提早终止了.(这是为了能释放应用程序资源. 该协议试图完成PEP325的生成器支持, 和其他平常的带有close()f方法的迭代器.)
注意: 应用程序必须在该迭代器生成第一个字符串前调用start_response对象, 以使得服务端能在发送任何具体内容前发送头内容.然而, 这个调用可以再迭代器首次迭代前被执行, 所以服务端必须假定start_response()在迭代前被调用了.
最后, 服务端不应该使用迭代器的任何其他属性, 除非它对服务端是一个专门制定的实例,例如由 wsgi.file_wrapper返回的"file_wrapper"(请看可选的特定平台文件处理Platform-Specific File Handling). 一般情况下, 只是用这里指定的属性, 或者是PEP234规定的迭代器接口.
分享到:
相关推荐
PEP8 规范也提供了忽略规则的建议,例如当遵循这份指南之后代码的可读性变差,或者与周围的代码保持一致(也可能出于历史原因),或者代码需要兼容不支持编码规范建议的老版本 Python。 结语 PEP8 编码规范中文版...
最后,PEP8规范也指出,如果遵循规范导致代码可读性变差,或者与周围的代码不一致,或者与老版本Python的兼容性有问题时,可以适当忽略某些规则。在这些情况下,最重要的是保持代码的清晰可读,以及与团队内部的风格...
PEP8是关于Python编码风格的规范,而如何将英文文档转成中文应当是遵循翻译原则和文档规范的问题。所以,正确理解这部分内容应该是:PEP8指导如何书写整洁的Python代码,并且在必要时如何将文档和注释用中文书写,...
PEP8编码规范,全称为“Style Guide for Python Code”,是由Python社区制定的一套编程风格指南,旨在提高Python代码的可读性和一致性。自2001年首版发布以来,经过多次修订和完善,PEP8已成为Python编程中普遍遵循...
**Python PEP 8编码规范中文版** Python PEP 8是Python编程语言的官方风格指南,由Guido van Rossum(Python之父)在2001年提出。PEP 8全称为"Python Enhancement Proposal 8",是Python社区广泛接受的代码风格指南...
"PEP8 Python 编码规范" PEP8 是 Python 官方提供的一份编码规范指南,旨在提高 Python 代码的可读性、可维护性和可重用性。本文档将对 PEP8 中的各种规范进行详细解释和总结。 缩进和换行 PEP8 规定使用 4 个...
### PEP8代码规范详解 #### 一、引言 PEP8,即Python Enhancement Proposal 8,是一份详尽的文档,旨在规定Python代码的编写标准与规范。PEP8由Guido van Rossum等人制定,其核心目标是通过一系列明确的规则提升...
参照https://legacy.python.org/dev/peps/pep-0008/官方文档简单总结了python编程过程中应当遵守的编码规范,比如函数的命名、变量的命名等(PEP8编码规范)。
### Python PEP8编码规范详解 #### 一、引言 PEP 8,全称为“Python Code Style Guide”,是Python官方推荐的编程规范指南。它由Python之父Guido van Rossum与其他开发者共同制定,并于2001年7月5日首次发布。PEP ...
PEP8是Python编程语言的官方编码规范,它由Python的核心开发者Guido van Rossum、Barry Warsaw和Nick Coghlan等人撰写和维护,最初于2001年发布。PEP是Python Enhancement Proposals的缩写,意为Python增强提案。PEP...
Python编程规范PEP8是一个由Python社区广泛认可和遵循的编程风格指南。它的全称是“Style Guide for Python Code”,旨在提升Python代码的可读性和一致性。PEP8最初由Python的创始人Guido van Rossum、Barry Warsaw...
### PEP8编码规范中文详解 #### 一、引言 PEP8 是 Python 编程语言中的一个重要标准,它提供了官方推荐的代码格式化规范。PEP8 的全称是“Python Enhancement Proposal 8”,它由 Guido van Rossum 和 Barry ...
### Python PEP8 代码规范详解 #### 一、引言 本文档旨在提供一份详细的指南,帮助开发者理解和遵循Python编程语言中广泛接受的最佳实践——PEP8代码规范。PEP8是Python Enhancement Proposal(Python改进提案)的...
Meinheld完全遵循PEP333和PEP3333规范,这些是WSGI接口的标准定义。 **PEP333**,全称Python Enhancements Proposal 333,由Gustaf Niemeyer提出,首次定义了WSGI的基本概念和接口,规定了Web服务器如何处理请求...
PEP5小学英语五年级下册课文及翻译.pdf
**Python PEP 8编码规范中文版** Python PEP 8是Python编程语言的官方编码风格指南,由Guido van Rossum(Python之父)在2001年提出,旨在提升代码的可读性和一致性。这个规范对于任何Python开发者来说都极其重要,...