Python Web开发中的WSGI协议简介(5)

application是一个定义了__call__方法的WSGIHandler类实例,首先加载中间件,然后根据environ生成请求request,根据请求生成响应response,status和response_headers由start_response处理,然后返回响应body。

Django也自带了WSGI server,当然性能不够好,一般用于测试用途,运行runserver命令时,Django可以起一个本地WSGI server,django/core/servers/basehttp.py文件:

def run(addr, port, wsgi_handler, ipv6=False, threading=False, server_cls=WSGIServer):

    server_address = (addr, port)

    if threading:

        httpd_cls = type('WSGIServer', (socketserver.ThreadingMixIn, server_cls), {})

    else:

        httpd_cls = server_cls

    httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)

    if threading:

        httpd.daemon_threads = True

    httpd.set_app(wsgi_handler)

    httpd.serve_forever() 

实现的WSGIServer,继承自wsgiref:

class WSGIServer(simple_server.WSGIServer):

    """BaseHTTPServer that implements the Python WSGI protocol"""

 

    request_queue_size = 10

 

    def __init__(self, *args, ipv6=False, allow_reuse_address=True, **kwargs):

        if ipv6:

            self.address_family = socket.AF_INET6

        self.allow_reuse_address = allow_reuse_address

        super().__init__(*args, **kwargs)

 

    def handle_error(self, request, client_address):

        if is_broken_pipe_error():

            logger.info("- Broken pipe from %s\n", client_address)

        else:

            super().handle_error(request, client_address)

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/5a18f0a52638df07849bb2b45c5090b7.html