Python Web开发:从 wsgi 开始

本文参考了:python

想要理解wsgi,首先得区分一个概念:server 和 app。nginx

此图来源于:www.toptal.com/python/pyth…django

uwsgi、gunicorn是 server,咱们写的 django、flask、sanic 程序是 app。app 就是一个可被调用的对象(callable object),server 会解析请求数据传给 app,app 运行业务逻辑以后,把结果返回给 server。flask

现实生活中,咱们部署的时候,可能还会在 server 前面加上一个 nginx,因此整个流程简单来讲是这样的:bash

app 可嵌套 -> 中间件

app 是一个可调用对象,这意味着我能够在 app1里面调用 app2,app2里面再调用 app3,这样一层一层嵌套下去。这不就是 middleware 吗?微信

若是你看过 django middleware 的源码,会看到MiddlewareMixin这个类:cookie

class MiddlewareMixin:
    def __init__(self, get_response=None):
        self.get_response = get_response
        super().__init__()

    def __call__(self, request):
        response = None
        if hasattr(self, 'process_request'):
            response = self.process_request(request)
        response = response or self.get_response(request)
        if hasattr(self, 'process_response'):
            response = self.process_response(request, response)
        return response
复制代码

定义了一个__call__方法,它是一个可调用对象。session

你在 django 配置文件中定义的:app

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
复制代码

运行的时候,就是这样一个一个地调用 middleware,直到调用到你的业务逻辑代码(最终的 app 部分)。函数

后面会再详细讲讲中间件开发。

app向server暴露的接口

app 是一个可调用的对象,它须要接收一些参数,具体以下:

def app(environ,start_response):
    pass
复制代码

具体看一下这两个参数:

  • environ,就是一个保护请求信息的字典。

好比 server 收到GET http://localhost:8000/auth?user=obiwan&token=123这条请求后,会生成下面这样一个 environ 字典:

这里面包含了这次请求的全部必要信息,经过这个字典,app就能知道此次请求的 path 是/auth,因而就知道该调用哪一个 handler 函数。还能经过 HTTP_COOKIE知道 cookie 值,而后能够定位到具体的用户。

  • start_response(status, headers,errors)

Server 传给 app 的回调函数,返回数据给 server 以前须要先调用这个回调函数,通知 server 你该来获取返回数据了。

据说这个参数实已经快有被废弃了,不须要彻底了解。下图来源于:WSGI: The Server-Application Interface for Python底部评论区。

若是你像我同样真正热爱计算机科学,喜欢研究底层逻辑,欢迎关注个人微信公众号:

相关文章
相关标签/搜索