码迷,mamicode.com
首页 > 编程语言 > 详细

python WSGI框架详解

时间:2019-05-17 23:09:56      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:response   设计者   port   http协议   接收   nload   哲学   djang   seh   

python WSGI框架详解

 

WSGI

 

几个关于WSGI相关的概念

WSGI:全称是Web Server Gateway Interface,WSGI不是服务器,python模块,框架,API或者任何软件,只是一种规范,描述web server如何与web application通信的规范。server和application的规范在PEP 3333中有具体描述。要实现WSGI协议,必须同时实现web server和web application,当前运行在WSGI协议之上的web框架有Torando,Flask,Django

uwsgi:与WSGI一样是一种通信协议,是uWSGI服务器的独占协议,用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型的描述,与WSGI协议是两种东西,据说该协议是fcgi协议的10倍快。

uWSGI:是一个web服务器,实现了WSGI协议、uwsgi协议、http协议等。

 

 

PEP 0333 – Python Web Server Gateway Interface 是一种 web server or gateway 和 python web application or framework 之间简单通用的接口,符合这种接口的 application 可运行在所有符合该接口的 server 上。通俗的讲,WSGI 规范了一种简单的接口,解耦了 server 和 application,使得双边的开发者更加专注自身特性的开发。

 

WSGI协议主要包括serverapplication两部分:

  • Web server/gateway: 即 HTTP Server,处理 HTTP 协议,接受用户 HTTP 请求和提供并发,调用 web application 处理业务逻辑。通常采用 C/C++ 编写,代表:apache, nginx 和 IIS。WSGI server负责从客户端接收请求,将request转发给application,将application返回的response返回给客户端;
  • Python Web application/framework: WSGI application接收由server转发的request,处理请求,并将处理结果返回给server。application中可以包括多个栈式的中间件(middlewares),这些中间件需要同时实现server与application,因此可以在WSGI服务器与WSGI应用之间起调节作用:对服务器来说,中间件扮演应用程序,对应用程序来说,中间件扮演服务器。
  • WSGI协议其实是定义了一种server与application解耦的规范,即可以有多个实现WSGI server的服务器,也可以有多个实现WSGI application的框架,那么就可以选择任意的server和application组合实现自己的web应用。例如uWSGI和Gunicorn都是实现了WSGI server协议的服务器,Django,Flask是实现了WSGI application协议的web框架,可以根据项目实际情况搭配使用。

技术图片

 

 

Application/Framework

Application/framework 端必须定义一个 callable object,callable object 可以是以下三者之一:

  • function, method
  • class
  • instance with a __call__ method

Callable object 必须满足以下两个条件:

  • 接受两个参数:字典(environ),回调函数(start_response,返回 HTTP status,headers 给 web server)
  • 返回一个可迭代的值

基于 callable function 的 application/framework 样例如下:

def application(environ, start_response):
    start_response(200 OK, [(Content-Type, text/plain)])
    return [This is a python application!]

基于 callable class 的 application/framework 样例如下:

class ApplicationClass(object):
    def __init__(self, environ, start_response):
        self.environ = environ
        self.start_response = start_response
 
    def __iter__(self):
        self.start_response(200 OK, [(Content-type, text/plain)])
        yield "Hello world!n"

 

 

Server/Gateway 

Server/gateway 端主要专注 HTTP 层面的业务,重点是接收 HTTP 请求和提供并发。每当收到 HTTP 请求,server/gateway 必须调用 callable object:

  • 接收 HTTP 请求,但是不关心 HTTP url, HTTP method 等
  • 为 environ 提供必要的参数,实现一个回调函数 start_response,并传给 callable object
  • 调用 callable object

我们直接使用支持 WSGI 框架的 wsgiref 库,编写一个样例:

# application/framework side
def application(environ, start_response):
    start_response(200 OK, [(Content-Type, text/plain)])
    return [This is a python application!]
 
# server/gateway side
if __name__ == __main__:
    from wsgiref.simple_server import make_server
    server = make_server(0.0.0.0, 8080, application)
    server.serve_forever()

 

Middleware: Components that Play Both Sides

Unix philosophy: do one thing and do it well.

技术图片

Middleware 处于 server/gateway 和 application/framework 之间,对 server/gateway 来说,它相当于 application/framework;对 application/framework 来说,它相当于 server/gateway。每个 middleware 实现不同的功能,我们通常根据需求选择相应的 middleware 并组合起来,实现所需的功能。比如,可在 middleware 中实现以下功能:

  • 根据 url 把用户请求调度到不同的 application 中。
  • 负载均衡,转发用户请求
  • 预处理 XSL 等相关数据
  • 限制请求速率,设置白名单

 技术图片

WSGI 的 middleware 体现了 unix 的哲学之一:do one thing and do it well。事实上,在定义 WSGI 框架的时候,设计者就要求 server/gateway 和 application/framework 双方尽可能的简单,同时也要求 middleware 设计的简单而专一,PEP 333 提到:

If middleware can be both simple and robust, and WSGI is widely available in servers and frameworks, it allows for the possibility of an entirely new kind of Python web application framework: one consisting of loosely-coupled WSGI middleware components.

本例实现了一个 IPBlacklist 的 middleware:

 
class IPBlacklistMiddleware(object):
    def __init__(self, app):
        self.app = app
 
    def __call__(self, environ, start_response):
        ip_addr = environ.get(HTTP_HOST).split(:)[0]
        if ip_addr not in (127.0.0.1):
            return forbidden(start_response)
 
        return self.app(environ, start_response)
 
def forbidden(start_response):
    start_response(403 Forbidden, [(Content-Type, text/plain)])
    return [Forbidden]
 
def application(environ, start_response):
    start_response(200 OK, [(Content-Type, text/plain)])
    return [This is a python application!]
 
if __name__ == __main__:
    from wsgiref.simple_server import make_server
    application = IPBlacklistMiddleware(application)
    server = make_server(0.0.0.0, 8080, application)
    server.serve_forever()

测试如下:

# 从本机测试
$ curl 127.0.0.1:8080/test
This is a python application!
 
# 从其它主机测测试                                                                                                                                                                                                 
$ curl 10.10.10.2:8080/test                                                                                                                                                                                                                                    
Forbidden

 

 

 

Path Dispatching

 

至此样例的一个不足之处是对于任意的 url 和 method,程序的返回值均为 ‘This is a python application!’,所以我们需要增加 path dispatch 功能。由于 WSGI 框架下的 server/gateway 不处理 url 和 method,所以 url mapping 需由 application/framework 端完成。注意到参数 environ,它包含以下变量:

  • REQUEST_METHOD: 即 HTTP method
  • PATH_INFO: 即 HTTP url

所以 application/framework 可以根据 environ 的 REQUEST_METHOD 和 PATH_INFO 实现 path dispatch,样例如下:

 
class IPBlacklistMiddleware(object):
    def __init__(self, app):
        self.app = app
 
    def __call__(self, environ, start_response):
        ip_addr = environ.get(HTTP_HOST).split(:)[0]
        if ip_addr not in (127.0.0.1):
            return forbidden(start_response)
 
        return self.app(environ, start_response)
 
def dog(start_response):
    start_response(200 OK, [(Content-Type, text/plain)])
    return [This is dog!]
 
def cat(start_response):
    start_response(200 OK, [(Content-Type, text/plain)])
    return [This is cat!]
 
def not_found(start_response):
    start_response(404 NOT FOUND, [(Content-Type, text/plain)])
    return [Not Found]
 
def forbidden(start_response):
    start_response(403 Forbidden, [(Content-Type, text/plain)])
    return [Forbidden]
 
def application(environ, start_response):
    path = environ.get(PATH_INFO, ‘‘).lstrip(/)
    mapping = {dog: dog, cat: cat}
 
    call_back = mapping[path] if path in mapping else not_found
    return call_back(start_response)
 
if __name__ == __main__:
    from wsgiref.simple_server import make_server
    application = IPBlacklistMiddleware(application)
    server = make_server(0.0.0.0, 8080, application)
    server.serve_forever()

测试如下:

$ curl 127.0.0.1:8080/dog
This is dog!                                                                                                                                                                                                                                                                   
 
$ curl 127.0.0.1:8080/cat
This is cat!                                                                                                                                                                                                                                                                   
 
$ curl 127.0.0.1:8080/monkey
Not Found

 

 

 

Django框架分析WSGI

下面我们以django为例,分析一下wsgi的整个流程

 

django WSGI application

WSGI application应该实现为一个可调用iter对象,例如函数、方法、类(包含**call**方法)。需要接收两个参数:一个字典,该字典可以包含了客户端请求的信息以及其他信息,可以认为是请求上下文,一般叫做environment(编码中多简写为environ、env),一个用于发送HTTP响应状态(HTTP status)、响应头(HTTP headers)的回调函数,也就是start_response()。通过回调函数将响应状态和响应头返回给server,同时返回响应正文(response body),响应正文是可迭代的、并包含了多个字符串。

下面是Django中application的具体实现部分:

class WSGIHandler(base.BaseHandler): 
   initLock = Lock() 
   request_class = WSGIRequest 
   def __call__(self, environ, start_response): 
   # 加载中间件 
    if self._request_middleware is None: 
         with self.initLock: 
             try: # Check that middleware is still uninitialized. 
                 if self._request_middleware is None: 
                    self.load_middleware() 
             except: # Unload whatever middleware we got 
                    self._request_middleware = None raise          
     set_script_prefix(get_script_name(environ)) # 请求处理之前发送信号   
     signals.request_started.send(sender=self.__class__, environ=environ) 
     try: 
          request = self.request_class(environ)  
     except UnicodeDecodeError: 
           logger.warning(Bad Request (UnicodeDecodeError),exc_info=sys.exc_info(), extra={status_code: 400,}
           response = http.HttpResponseBadRequest() 
     else: 
           response = self.get_response(request) 
     response._handler_class = self.__class__ status = %s %s % (response.status_code, response.reason_phrase) 
     response_headers = [(str(k), str(v)) for k, v in response.items()] for c in response.cookies.values(): response_headers.append((str(Set-Cookie), str(c.output(header=‘‘)))) 
     # server提供的回调方法,将响应的header和status返回给server     
     start_response(force_str(status), response_headers) 
     if getattr(response, file_to_stream, None) is not None and environ.get(wsgi.file_wrapper): 
          response = environ[wsgi.file_wrapper](response.file_to_stream) 
     return response

 

django WSGI Server

负责获取http请求,将请求传递给WSGI application,由application处理请求后返回response。以Django内建server为例看一下具体实现。通过runserver运行django
项目,在启动时都会调用下面的run方法,创建一个WSGIServer的实例,之后再调用其serve_forever()方法启动服务。

 

python WSGI框架详解

标签:response   设计者   port   http协议   接收   nload   哲学   djang   seh   

原文地址:https://www.cnblogs.com/-wenli/p/10884168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!