标签:div file script item rap color 参数 自身 流程
web应用的最原始的访问流程:
脱离底层HTTP协议层内容,只关注如何生成HTML文档部分,并定义了一个标准:Web Server Gateway Interface。重点要求实现一个能够接收两个参数的可调用对象(单独的函数,或者类中的__call__函数)
class WSGIHandler(object): def __init__(): pass def __call__(self, environ, start_response): request = environ start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘), ]) return ‘Hello World‘ app = WSGIHandler()
def app(environ, start_response): request = environ start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘),]) return ‘Hello World‘
程序都存在输入、输出,WSGI定义了输出为:响应头和响应体,并且定义了输入:environ和start_response,如果使用的是HTTP、NGINX等web服务器,则需要安装对应的WSGI模块,该模块能够处理WSGI标准所要求的输入输出。Python自身也提供了纯Python实现的wsgi功能模块:wsgiref,这个模块会处理输入问题,并将app返回的值最终通过HTTP发送给客户端。示例:
from wsgiref.simple_server import make_server def application(environ, start_response): request = environ start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘),]) return ‘Hello, world‘ print("Start server: 0.0.0.0:8080") httpd = make_server(‘0.0.0.0‘, 8080, app) httpd.serve_forever()
web框架的核心是无法脱离WSGI的逻辑部分的,django框架的入口程序:django/core/handlers/wsgi.py#WSGIHandler.__call__函数,函数代码(版本v1.11.6):
class WSGIHandler(base.BaseHandler): request_class = WSGIRequest def __init__(self, *args, **kwargs): super(WSGIHandler, self).__init__(*args, **kwargs) self.load_middleware() def __call__(self, environ, start_response): set_script_prefix(get_script_name(environ)) signals.request_started.send(sender=self.__class__, environ=environ) request = self.request_class(environ) response = self.get_response(request)
response._handler_class = self.__class__
status = ‘%d %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=‘‘))) 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
[TimLinux] Python Django与WSGI的简介
标签:div file script item rap color 参数 自身 流程
原文地址:https://www.cnblogs.com/timlinux/p/9353720.html