Differences between revisions 13 and 17 (spanning 4 versions)
Revision 13 as of 2006-08-24 04:52:41
Size: 12525
Editor: ZoomQuiet
Comment:
Revision 17 as of 2006-08-24 04:57:45
Size: 856
Editor: ZoomQuiet
Comment:
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
[[Include(AbtWSGI)]] [[Include(/AbtWSGI)]]
Line 18: Line 18:

应用程序对象只是一个接受两个参数的 callable 对象而已. 别把术语 "对象" 误解为真的需要一个实例对象: 一个函数, 方法, 类, 或者实现了 {{{__call__}}} 方法的实例对象都可以用来作为应用程序对象. 应用程序对象必须能够被重复调用, 因为实际上所有 服务器(除了CGI) 都会发出这样的重复的请求.

The application object is simply a callable object that accepts
two arguments. The term "object" should not be misconstrued as
requiring an actual object instance: a function, method, class,
or instance with a ``__call__`` method are all acceptable for
use as an application object. Application objects must be able
to be invoked more than once, as virtually all servers/gateways
(other than CGI) will make such repeated requests.

(注意: 虽然我们把它说成是一个 "应用程序" 对象, 但也别误解为应用程序开发者会把 WSGI 当一个 web编程API 来用!
我们假定应用程序开发者仍然使用现有的,高层的框架服务来开发他们的应用程序. WSGI 是一个给框架和服务器开发者用的工具,
并且不会提供对应用程序开发者的直接支持.)

(Note: although we refer to it as an "application" object, this
should not be construed to mean that application developers will use
WSGI as a web programming API! It is assumed that application
developers will continue to use existing, high-level framework
services to develop their applications. WSGI is a tool for
framework and server developers, and is not intended to directly
support application developers.)

###[#head-719d788ab1dd4f4bb61a26f59256d3a0dfc66cde-2 示例]

=== 示例 ===
{{{#!python
def simple_app(environ, start_response):
    """Simplest possible application object"""
    status = '200 OK'
    response_headers = [('Content-type','text/plain')]
    start_response(status, response_headers)
    return ['Hello world!\n']


class AppClass:
    """Produce the same output, but using a class

    (Note: 'AppClass' is the "application" here, so calling it
    returns an instance of 'AppClass', which is then the iterable
    return value of the "application callable" as required by
    the spec.

    If we wanted to use *instances* of 'AppClass' as application
    objects instead, we would have to implement a '__call__'
    method, which would be invoked to execute the application,
    and we would need to create an instance for use by the
    server or gateway.
    """

    def __init__(self, environ, start_response):
        self.environ = environ
        self.start = start_response

    def __iter__(self):
        status = '200 OK'
        response_headers = [('Content-type','text/plain')]
        self.start(status, response_headers)
        yield "Hello world!\n"
}}}
[[Include(/AppWSGI)]]
Line 80: Line 21:
[[Include(/SrvWSGI)]]
=== WSGI中间件 ===
[[Include(/MidWSGI)]]
Line 81: Line 25:
服务器 或 getway 每次收到 http客户端 发出的请求都会调用一次相应的 应用程序callable .
举例来说, 这里有一个[#head-42263f1a3a5d0f4d19ac223b3f534410d2aaeb62-2 简单的 CGI getway],
以一个接受一个应用程序对象作为参数的函数来实现的.
值得注意的是这个简单的例子还拥有有限的错误处理功能,
因为未捕捉的异常默认会写到 {{{sys.error}}} 里并被web服务器记录下来.

The server or gateway invokes the application callable once for each
request it receives from an HTTP client, that is directed at the
application. To illustrate, here is a [#head-42263f1a3a5d0f4d19ac223b3f534410d2aaeb62-2 simple CGI gateway], implemented
as a function taking an application object. Note that this simple
example has limited error handling, because by default an uncaught
exception will be dumped to ``sys.stderr`` and logged by the web
server.

###[#head-747ebe56750a1de964d165b5b4a56e9eeb2f74a4-2 示例]
=== 示例 ===
{{{#!python
import os, sys

def run_with_cgi(application):

    environ = dict(os.environ.items())
    environ['wsgi.input'] = sys.stdin
    environ['wsgi.errors'] = sys.stderr
    environ['wsgi.version'] = (1,0)
    environ['wsgi.multithread'] = False
    environ['wsgi.multiprocess'] = True
    environ['wsgi.run_once'] = True

    if environ.get('HTTPS','off') in ('on','1'):
        environ['wsgi.url_scheme'] = 'https'
    else:
        environ['wsgi.url_scheme'] = 'http'

    headers_set = []
    headers_sent = []

    def write(data):
        if not headers_set:
             raise AssertionError("write() before start_response()")

        elif not headers_sent:
             # Before the first output, send the stored headers
             status, response_headers = headers_sent[:] = headers_set
             sys.stdout.write('Status: %s\r\n' % status)
             for header in response_headers:
                 sys.stdout.write('%s: %s\r\n' % header)
             sys.stdout.write('\r\n')

        sys.stdout.write(data)
        sys.stdout.flush()

    def start_response(status,response_headers,exc_info=None):
        if exc_info:
            try:
                if headers_sent:
                    # Re-raise original exception if headers sent
                    raise exc_info[0], exc_info[1], exc_info[2]
            finally:
                exc_info = None # avoid dangling circular ref
        elif headers_set:
            raise AssertionError("Headers already set!")

        headers_set[:] = [status,response_headers]
        return write

    result = application(environ, start_response)
    try:
        for data in result:
            if data: # don't send headers until body appears
                write(data)
        if not headers_sent:
            write('') # send headers now if body was empty
    finally:
        if hasattr(result,'close'):
            result.close()
}}}

=== WSGI中间件 ===

注意一个对象可以在一些应用程序面前是服务器, 而从一些服务器看来却是应用程序. 这样的 "中间件" 可以实现以下一些功能:

Note that a single object may play the role of a server with respect
to some application(s), while also acting as an application with
respect to some server(s). Such "middleware" components can perform
such functions as:

 * 可以根据目的 URL 将一个请求分发 (routing) 给不同的应用程序对象, 并对 {{{environ}}} 做相应修改.

 * Routing a request to different application objects based on the
  target URL, after rewriting the ``environ`` 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

 * 对内容进行后加工, 比如应用 XSL 样式.

 * Perform content postprocessing, such as applying XSL stylesheets

通常中间件的存在对 服务器 和 应用程序 两边的接口都是透明的, 并且应该不需要提供什么特殊的支持. 用户如果想在应用程序中组合一个中间件, 只需简单地把中间件当个应用程序提供给服务器, 并配置该中间件, 让它以服务器的身份来调用应用程序. 当然中间件包装的这个应用程序实际上可能是一个包装着另一个应用程序的中间件, 如此反复, (以至无穷), 最终形成了传说中的 "中间件栈" .

The presence of middleware in general is transparent to both the
"server/gateway" and the "application/framework" sides of the
interface, and should require no special support. A user who
desires to incorporate middleware into an application simply
provides the middleware component to the server, as if it were
an application, and configures the middleware component to
invoke the application, as if the middleware component were a
server. Of course, the "application" that the middleware wraps
may in fact be another middleware component wrapping another
application, and so on, creating what is referred to as a
"middleware stack".

在多数情况下, 中间件必须同时遵守WSGI服务器和WSGI应用程序两边的限制和需要.
而且在某些情况下, 对中间件的要求比一个纯粹服务器或应用程序还要严格, 这几个方面在规范中还会涉及到.

For the most part, middleware must conform to the restrictions
and requirements of both the server and application sides of
WSGI. In some cases, however, requirements for middleware
are more stringent than for a "pure" server or application,
and these points will be noted in the specification.

这里有一个 (随手写的) 中间件的例子, 它将 {{{text/plain}}} 的响应转换成 pig latin, 用到 Joe Strout 的 {{{piglatin.py}}}.
(注意: 一个 "真实" 的中间件组件很可能会使用一种更成熟的方式来检查 content type, 而且还应该检查一下 content encoding.
另外, 这个简单的例子还忽略了一个单词可能会在数据块的边界处被分割.)

Here is a (tongue-in-cheek) example of a middleware component that
converts ``text/plain`` responses to pig latin, using Joe Strout's
``piglatin.py``. (Note: a "real" middleware component would
probably use a more robust way of checking the content type, and
should also check for a content encoding. Also, this simple
example ignores the possibility that a word might be split across
a block boundary.)

###[#head-229f9e55e6acb5e474a68f7293b978f55ff3beb5-2 示例]
=== 示例 ===
{{{#!python
from piglatin import piglatin

class LatinIter:

    """Transform iterated output to piglatin, if it's okay to do so

    Note that the "okayness" can change until the application yields
    its first non-empty string, so 'transform_ok' has to be a mutable
    truth value."""

    def __init__(self,result,transform_ok):
        if hasattr(result,'close'):
            self.close = result.close
        self._next = iter(result).next
        self.transform_ok = transform_ok

    def __iter__(self):
        return self

    def next(self):
        if self.transform_ok:
            return piglatin(self._next())
        else:
            return self._next()

class Latinator:

    # by default, don't transform output
    transform = False

    def __init__(self, application):
        self.application = application

    def __call__(self, environ, start_response):

        transform_ok = []

        def start_latin(status,response_headers,exc_info=None):

            # Reset ok flag, in case this is a repeat call
            transform_ok[:]=[]

            for name,value in response_headers:
                if name.lower()=='content-type' and value=='text/plain':
                    transform_ok.append(True)
                    # Strip content-length if present, else it'll be wrong
                    response_headers = [(name,value)
                        for name,value in response_headers
                            if name.lower()<>'content-length'
                    ]
                    break

            write = start_response(status,response_headers,exc_info)

            if transform_ok:
                def write_latin(data):
                    write(piglatin(data))
                return write_latin
            else:
                return write

        return LatinIter(self.application(environ,start_latin),transform_ok)


# Run foo_app under a Latinator's control, using the example CGI gateway
from foo_app import foo_app
run_with_cgi(Latinator(foo_app))
}}}

TableOfContents

简介

Include(/AbtWSGI)

相关页面

规范

WSGI应用程序

Include(/AppWSGI)

WSGI服务器

Include(/SrvWSGI)

WSGI中间件

Include(/MidWSGI)

规范细节

Include(/DetailsWSGI)

讨论

WSGI (last edited 2009-12-25 07:17:11 by localhost)