Django源码分析(1) —— WSGI协议

一、概念介绍

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

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

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


WSGI协议主要包括serverapplication两部分: WSGI server负责从客户端接收请求,将request转发给application,将application返回的response返回给客户端; 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框架,可以根据项目实际情况搭配使用。

二、协议实现

1、Web Server部分实现:Web server针对从HTTP客户端接收的每个request调用一次Web application。为了说明这一点,这里有一个简单的CGI网关服务的函数实现。

py 复制代码
import os, sys

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

def unicode_to_wsgi(u):
    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!")

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

2、Web Application 部分的实现:Web application是一个接受两个参数的可调用对象,函数、方法、类或具有 __call__方法的实例都可以用作application对象。Web application对象必须能够被多次调用,因为几乎所有服务器/网关都会发出重复请求。

py 复制代码
HELLO_WORLD = b"Hello world!\n"

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]

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
相关推荐
倔强青铜三1 分钟前
苦练Python第72天:colorsys 模块 10 分钟入门,让你的代码瞬间“好色”!
人工智能·python·面试
胖哥真不错1 小时前
Python基于PyTorch实现多输入多输出进行BP神经网络回归预测项目实战
pytorch·python·毕业设计·论文·毕设·多输入多输出·bp神经网络回归预测
合作小小程序员小小店1 小时前
web网页开发,旧版在线%考试,判题%系统demo,基于python+flask+随机分配考试题目,基于开发语言python,数据库mysql
开发语言·后端·python·mysql·flask·html5
dalalajjl2 小时前
爱派(AiPy):一个让大语言模型直接操作Python完成任务
python
景彡先生2 小时前
Python matplotlib详解:从入门到精通,数据可视化利器
python·信息可视化·matplotlib
程序员大雄学编程2 小时前
用Python来学微积分22-费马定理
人工智能·python·数学·微积分
deephub2 小时前
sklearn 特征选择实战:用 RFE 找到最优特征组合
人工智能·python·机器学习·sklearn·特征选择
程序员杰哥3 小时前
外包干了三年,快要废了。。。
自动化测试·软件测试·python·功能测试·测试工具·面试·职场和发展
咩?3 小时前
10.27-yolov5代码
人工智能·python·深度学习
gfdgd xi3 小时前
好消息:KMRE 安卓模拟器现可在Debian/GXDE使用
android·python·架构·bug·deepin·龙芯