WSGI详解汇总

WSGI是什么
WSGI(Web Server Gateway Interface)是一种规范
参见PEP 333 http://www.python.org/dev/peps/pep-0333/


WSGI Server有哪些
比如 Django、CherryPy 都自带 WSGI server 主要是测试用途, 发布时则使用生产环境的 WSGI server
而有些 WSGI 下的框架比如 pylons、bfg 等, 自己不实现 WSGI server。使用 paste 作为 WSGI server
CherryPy's WSGI server.


wsgi有两方,服务器方 和 应用程序

①服务器方:其调用应用程序,给应用程序提供(环境信息)和(回调函数), 这个回调函数是用来将应用程序设置的http header和status等信息传递给服务器方.


②应用程序:用来生成返回的header,body和status,以便返回给服务器方

     Python语言写的一个符合WSGI的“Hello World”应用程序如下所示:

<span style="color:#464646;word-wrap: normal; word-break: normal; line-height: 18px;"><span style="word-wrap: normal; word-break: normal; font-weight: bold;">def</span> app<span style="word-wrap: normal; word-break: normal;">(</span>environ<span style="word-wrap: normal; word-break: normal;">,</span> start_response<span style="word-wrap: normal; word-break: normal;">)</span>:
<span style="color:#464646;word-wrap: normal; word-break: normal;"> <wbr>  <wbr> <wbr></wbr></wbr></wbr></span> start_response<span style="word-wrap: normal; word-break: normal;">(</span><span style="word-wrap: normal; word-break: normal;">'200 OK'</span><span style="word-wrap: normal; word-break: normal;">,</span> <span style="word-wrap: normal; word-break: normal;">[</span><span style="word-wrap: normal; word-break: normal;">(</span><span style="word-wrap: normal; word-break: normal;">'Content-Type'</span><span style="word-wrap: normal; word-break: normal;">,</span> <span style="word-wrap: normal; word-break: normal;">'text/plain'</span><span style="word-wrap: normal; word-break: normal;">)</span><span style="word-wrap: normal; word-break: normal;">]</span><span style="word-wrap: normal; word-break: normal;">)
<span style="color:#464646;word-wrap: normal; word-break: normal;"> <wbr>  <wbr> <wbr></wbr></wbr></wbr></span></span> <span style="word-wrap: normal; word-break: normal; font-weight: bold;">yield</span> <span style="word-wrap: normal; word-break: normal;">"Hello world!<span style="word-wrap: normal; word-break: normal; font-weight: bold;">\n</span>"</span></span>

其中

  • 第一行定义了一个名为app的应用程序,接受两个参数,environ和 start_response,environ是一个字典包含了CGI中的环境变量,start_response也是一个callable,接受两个必 须的参数,status(HTTP状态)和response_headers(响应消息的头)。
  • 第二行调用了start_response,状态指定为“200 OK”,消息头指定为内容类型是“text/plain”
  • 第三行将响应消息的消息体返回。

   那通俗点来将的话:
wsgi中的服务器方:我们可以理解成是webserver,当然这个webserver可以是外置的,比如lighttpd,也可以是python自己写的。
而应用程序说白了就是:请求的统一入口!所有的请求都进入到这个app中来处理! 这个app说白了就是一个函数!!(类中的__call__是一样的道理)

Python自带的 wsgiref

WSGI application

一个接口与两个参数

application(environ, start_response)
Demo

[python] view plaincopy

    #! /usr/bin/env python  
      
    # Our tutorial's WSGI server  
    from wsgiref.simple_server import make_server  
      
    def application(environ, start_response):  
      
       # Sorting and stringifying the environment key, value pairs  
       response_body = ['%s: %s' % (key, value)  
                        for key, value in sorted(environ.items())]  
       response_body = '\n'.join(response_body)  
      
       status = '200 OK'  
       response_headers = [('Content-Type', 'text/plain'),  
                      ('Content-Length', str(len(response_body)))]  
       start_response(status, response_headers)  
      
       return [response_body]  
      
    # Instantiate the WSGI server.  
    # It will receive the request, pass it to the application  
    # and send the application's response to the client  
    httpd = make_server(  
       'localhost', # The host name.  
       8051, # A port number where to wait for the request.  
       application # Our application object name, in this case a function.  
       )  
      
    httpd.serve_forever()  


wsgiref的simple_server说明
server的主要作用是接受client的请求,並把的收到的请求交給RequestHandlerClass处理,
RequestHandlerClass处理完成后回传结果给client

WSGIServer继承关系
WSGIServer-->HTTPServer-->SocketServer.TCPServer-->BaseServer
主要处理流程
serve_forever
  _handle_request_noblock()
    process_request
      finish_request--> RequestHandlerClass()
      close_request

serve_forever循环接受client请求, 如果有请求来,
经finish_request方法把请求交给RequestHandlerClass处理,
RequestHandlerClass调用handle()方法处理request,
WSGIRequestHandler的handle()方法把request又交給ServerHandler处理,
ServerHandler调用run執行application方法, 回传网页的结果(含http header及网页内容)给client

WSGIRequestHandler继承关系
WSGIRequestHandler-->BaseHTTPRequestHandler-->StreamRequestHandler-->BaseRequestHandler

BaseRequestHandler主要方法及处理流程
1、setup()
2、handle()
3、finish()

WSGIRequestHandler主要方法及处理流程
1、get_environ 增加env
2、handle (override)
        handler = ServerHandler
        handler.run(self.server.get_app())

ServerHandler继承关系
ServerHandler-->SimpleHandler-->BaseHandler
run方法
  setup_environ
  self.result = application(self.environ, self.start_response)
  self.finish_response
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值