wsgi (Python Web Server Gateway Interface) ******************

http://yuyang.farbox.com/articles/wsgi.html

 

wsgi (Python Web Server Gateway Interface)

简单的说wsgi是定义了一种规范,它可以使任何符合该规范的web应用(比如网站)都可 以运行在任何符合该规范的web server上,就像插座与插头,要想插头插入插孔中,你 就必须让插头的尺寸与插座中插孔的尺寸匹配,也就是既要规定插头的尺寸,也要规定 插孔的尺寸,同理为了实现使任何符合wsgi规范的web应用(比如网站)都可以运行在任 何符合wsgi规范的web server上的目的,wsgi也就必须既规范web应用同时也要规范web server,该规范在EPE 3333 中说明

The Application/Framework Side(web应用端)

这一端必须定义一个接受两个参数的可调用对象,可调用对象可以是函数,类方法, 类以及一个包含__call__的类实例,下面是pep3333的示例代码:

HELLO_WORLD = b"Hello world!\n"

def simple_app(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)
    return [HELLO_WORLD]

class AppClass:
    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

上面是两个代码示例,一个是使用函数,一个是使用类,对于类这里做一些说明,首 先调用AppClass(environ, start_response)就会返回一个AppClass实例,然后服务端 会对返回的实例调用for data in instsnce_of_AppClass,这样就会触发__iter__方法, 也就是返回HELLO_WORLD。注意在可调用对象中调用start_response时应该传递http头 中状态行以及http头部,在该实例中分别是'200OK', [('Content-Type': 'text/plain')], 而且应该返回一个可迭代对象(比如list或者有__iter__方法的类实 例), 而且这个可迭代对象的每一项都是HTTP报文body的内容

 

 

text/html & text/plain的区别

需要了解的概念

  Content-Type:用于定义用户的浏览器或相关设备如何显示将要加载的数据,或者如何处理将要加载的数据

  MIME:MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。

 

text/html的意思是将文件的content-type设置为text/html的形式,浏览器在获取到这种文件时会自动调用html的解析器对文件进行相应的处理。

text/plain的意思是将文件设置为纯文本的形式,浏览器在获取到这种文件时并不会对其进行处理。

 

The Server/Gateway Side (web server端)

在server指定的代码会在每一次http请求时调用。它主要是environ以及一个名为 start_response的函数传递给在web应用端定义的可调用对象,然后获得返回值

import os, sys

enc, esc = sys.getfilesystemencoding(), 'surrogateescape'

def unicode_to_wsgi(u):
    # Convert an environment variable to a WSGI "bytes-as-unicode" string
    return u.encode(enc, esc).decode('iso-8859-1')

def wsgi_to_bytes(s):
    return s.encode('iso-8859-1')

def run_with_cgi(application):
    environ = {k: unicode_to_wsgi(v) for k,v in os.environ.items()}
    environ['wsgi.input']        = sys.stdin.buffer
    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):
        out = sys.stdout.buffer

        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
            out.write(wsgi_to_bytes('Status: %s\r\n' % status))
            for header in response_headers:
                out.write(wsgi_to_bytes('%s: %s\r\n' % header))
                out.write(wsgi_to_bytes('\r\n'))

        out.write(data)
        out.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[1].with_traceback(exc_info[2])
                finally:
                    exc_info = None     # avoid dangling circular ref
                elif headers_set:
                    raise AssertionError("Headers already set!")

         ##设置html头
        headers_set[:] = [status, response_headers]
        # Note: error checking on the headers should happen here,
        # *after* the headers are set.  That way, if an error
        # occurs, start_response can only be re-called with
        # exc_info set.

        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()

上面的代码很清楚,result = application(environ, start_response)就是调用web 应用端指定的可调用对象,获得返回值赋给result,接着调用 for data in result 遍 历result,如果data不为空那么就调用write写入socket,注意write函数会检查是否已 经将http报文的头部写入了socket,如果没有那么就先写入报文头,然后写入data,记住 HTTP报文头是由web应用端调用start_response通过参数传递给server端的,而Http报 文 Body的部分是包含在可调用对象返回值中的也就是result中的

  1. start_response: 必须接受HTTP报文的status行以及header列表作为参数,还有一 个exec_info是可选参数,必须返回一个可调用对象(本例就是write函数),这个对象 接受一个byte string,并且将该byte string发送给客户端(比如浏览器),这样在 web应用端就可以可选的将内容写入客户度,本例的web应用端没有使用该功能
  2. environ 这只能是一个python字典,不能是字典的子类,这个字典包含了一个HTTP请 求的所有基本信息,这是pep3333的详细解释

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值