Flask werkzeug流程大概:执行run_simple ,实际执行为先用make_server 创建一个 BaseServer 实例,然后执行 实例的serve_forever 方法, serve_forever 调用 run_simple 传入的第三个参数,执行(self, environ, start_response) ,environ 为 初步处理的request 请求,start_response 为回调函数;
若第三个参数为 Response 的实例化对象,以下称obj,obj 是具体的request 入口,负责具体的逻辑,其他不同的框架其实是第三个参数不同,Flask由此而来。Flask 的三种方式
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple# 方式一:实例化Response,最后 self.request = encode('Hello World application1!')
application1 = Response('Hello World application1!')
#方式二
def application2(environ, start_response):
request = Request(environ)
response = Response("Hello %s!" % request.args.get('name', 'World!'))
return response(environ, start_response)
#方式三
@Request.application
def hello(request):
return Response('Hello World Request!')if __name__ == '__main__':
# run_simple('localhost', 4000, application1)
# run_simple('localhost', 4000, application2)
run_simple('localhost', 4000, hello)我们在浏览器输入http://localhost:4000/就会得到response信息
接下来我们就简单的分析下,该模块的请求、响应流程
werkzeug包下的__init__.py模块初始化时,遍历循环字典all_by_module,重新构造object_origins字典数据格式,该字典类型格式如下,我列举出来一些元素,以下是键值对形式
# BaseResponse - --- werkzeug.wrappers # BaseRequest - --- werkzeug.wrappers # Request - --- werkzeug.wrappers # Response - --- werkzeug.wrappers
该字典的键是werkzeug下的某模块中的函数、方法,值是werkzeug下的某模块中。我们回头看我们的demo示例,在文件起始处我们引入了from werkzeug.serving import run_simple。我们跟踪代码去看下serving.py模块下的run_simple函数
####################### serving.py 文件下的 run_simple 函数 ############################
# run_simple最后执行inner函数
def run_simple(hostname, port, application, use_reloader=False,use_debugger=False, use_evalex=True,
extra_files=None, reloader_interval=1,reloader_type='auto', threaded=False,
processes=1, request_handler=None, static_files=None,passthrough_errors=False, ssl_context=None)# 参数
hostname:应用程序的主机
port:端口
application:WSGI应用程序
use_reloader:如果程序代码修改,是否需要自动启动服务
use_debugger:程序是否要使用工具和调试系统
use_evalex:应用是否开启异常评估
extra_files:重载器应该查看的文件列表附加到模块。例如配置文件夹
reloader_interval:秒重载器的间隔
reloader_type:重载器的类型
threaded:进程是否处理单线程的每次请求
processes:如果大于1,则在新进程中处理每个请求。达到这个最大并发进程数
request_handler:可以自定义替换BaseHTTPRequestHandler
static_files:静态文件路径的列表或DICT
passthrough_errors:将此设置为“真”以禁用错误捕获。这意味着服务器会因错误而死亡
ssl_context:如何进行传输数据加密,可以设置的环境
# run_simple函数中,最后会执行到
if use_reloader:
# 省略,太长了,反正暂时也执行不到
else:
inner()
################ serving.py 文件下的 run_simple 函数下的 inner()函数 #####################
def inner():
try:
fd = int(os.environ['WERKZEUG_SERVER_FD'])
except (LookupError, ValueError):
fd = None
# 通过make_server方法,跟进我们在初始化__init__中的参数,去构造server服务
srv = make_server(hostname, port, application, threaded,processes, request_handler,passthrough_errors, ssl_context,fd=fd)
if fd is None:
log_startup(srv.socket)
# 把服务运行起来 serve_forever() 是HTTPserver的方法,
# 当有请求过来之后,server_forever会将run_simple()中的第三个参数加括号执行,并传入参数(environ, start_response)
srv.serve_forever()
############### BaseWSGIServer 类下的 serve_forever 方法 #######################
class BaseWSGIServer(HTTPServer, object)
def serve_forever(self):
self.shutdown_signal = False
try:
# class HTTPServer(socketserver.TCPServer)未实现 serve_forever ————>
# class TCPServer(BaseServer) 未实现 serve_forever :
# BaseServer 实现 serve_forever
HTTPServer.serve_forever(self)
except KeyboardInterrupt:
pass
finally:
self.server_close()
################ serving.py 文件下的 def make_server #####################
def make_server(host=None, port=None, app=None, threaded=False, processes=1,request_handler=None,
passthrough_errors=False,ssl_context=None, fd=None):
"""Create a new server instance that is either threaded, or forks
or just processes one request after another."""if threaded and processes > 1:
raise ValueError("cannot have a multithreaded and multi process server.")
elif threaded:
return ThreadedWSGIServer(host, port, app, request_handler,passthrough_errors, ssl_context, fd=fd)
elif processes > 1:
return ForkingWSGIServer(host, port, app, processes, request_handler,passthrough_errors, ssl_context, fd=fd)
else:
# 没想到吧,最后执行这个 ,实例化BaseWSGIServer
return BaseWSGIServer(host, port, app, request_handler,passthrough_errors, ssl_context, fd=fd)
########## socketserver.py 文件下的 class BaseServer 下的 serve_forever #######
# 具体的监听 socket ,当有请求到来时,执行传入的第三个 参数,执行格式为 函数(self, environ, start_response),并接受返回值
class BaseServer:
def serve_forever(self, poll_interval=0.5):
"""Handle one request at a time until shutdown.
Polls for shutdown every poll_interval seconds. Ignores
self.timeout. If you need to do periodic tasks, do them in
another thread.
"""
self.__is_shut_down.clear()
try:
# XXX: Consider using another file descriptor or connecting to the
# socket to wake this up instead of polling. Polling reduces our
# responsiveness to a shutdown request and wastes cpu at all other
# times.
with _ServerSelector() as selector: # 具体监听过程
selector.register(self, selectors.EVENT_READ)while not self.__shutdown_request:
ready = selector.select(poll_interval)
if ready:
self._handle_request_noblock()self.service_actions()
finally:
self.__shutdown_request = False
self.__is_shut_down.set()
我们看下我们的示例中,最简单那个例子application1 = Response('Hello World application1!'),设置run_simple('localhost', 4000, application1),当接受请求时,为什么会执行application1的对象内方法,并且返回给浏览器
在我们示例代码中,当run_simple('localhost', 4000, application1)执行后,实际执行inner(),而inner做了两件事,一件make_server,一件server_forver(一直在监听)。当有请求过来之后,server_forever会将run_simple()中的第三个参数加括号执行,并传入参数(environ, start_response)。所以当http://localhost:4000/请求时,就会触发并调用application1(),即application1 = Response('Hello World application1!').__call(self, environ, start_response)__
在所有的 python web 框架都要遵循 WSGI 协议,WSGI 中有一个非常重要的概念:
每个 python web 应用都是一个可调用(callable)的对象(如上述的Response),要运行 web 应用,必须有 web server,在werkzeug中提供了 WSGIServer,Server 和 Application 之间怎么通信,就是 WSGI 的功能
wsgi有两方,服务器方 和 应用程序
- 服务器方:调用应用程序,给应用程序传递(环境信息)和(回调函数), 这个回调函数是用来将应用程序设置的http header和status等信息传递给服务器方.
- 应用程序:用来生成返回的header,body和status,以便返回给服务器方。
看完了请求,接下来看下返回。即werkzeug.wrappers.py模块下的Response类
################### response.py 文件下的 class Response ################
class Response(BaseResponse, ETagResponseMixin, ResponseStreamMixin,CommonResponseDescriptorsMixin,WWWAuthenticateMixin):
# 就这么多,没了,气不气
该类是多重继承类,这里主要看下BaseResponse,先看下初始方法
#################### base_responce.py 文件下的 class BaseResponse(object) #############
class BaseResponse(object)def __init__(self, response=None, status=None, headers=None,mimetype=None, content_type=None, direct_passthrough=False):if isinstance(headers, Headers):self.headers = headerselif not headers:self.headers = Headers()else:self.headers = Headers(headers)if content_type is None:if mimetype is None and 'content-type' not in self.headers:mimetype = self.default_mimetypeif mimetype is not None:mimetype = get_content_type(mimetype, self.charset)content_type = mimetypeif content_type is not None:self.headers['Content-Type'] = content_typeif status is None:status = self.default_statusif isinstance(status, integer_types):self.status_code = statuselse:self.status = statusself.direct_passthrough = direct_passthroughself._on_close = []# we set the response after the headers so that if a class changes# the charset attribute, the data is set in the correct charset.if response is None:self.response = []elif isinstance(response, (text_type, bytes, bytearray)):self.set_data(response) #################看这里else:self.response = response
在BaseResponse类__init__初始方法中,我们定义了返回的Headers、content_type、状态码status,最后通过self.set_data(response),跟踪代码如下:
####### base_responce.py 文件下的 class BaseResponse(object) 下的 def set_data ##########
## 主要讲传入的应答 编码
class BaseResponse(object)def set_data(self, value):if isinstance(value, text_type):# 字符串编码value = value.encode(self.charset)else:value = bytes(value)self.response = [value] # 看这里,看这里if self.automatically_set_content_length:self.headers['Content-Length'] = str(len(value))
将我们示例中的application1 = Response('Hello World application1!')参数字符串,进行bytes类型转换并赋值给self.response,然后执行对象(),即调用__call__方法,
####### base_responce.py 文件下的 class BaseResponse(object) 下的 def __call__ ##########
# 这个方法的作用就是,执行 具体的请求过程,然后调用回调函数,并提供返回值给调用者 HTTPServer.serve_forever(self)
class BaseResponse(object)def __call__(self, environ, start_response):print(start_response)# get_wsgi_response ,是具体的请求处理过程,后面Flask源码解析会讲到app_iter, status, headers = self.get_wsgi_response(environ)# start_response ,提供的回调函数start_response(status, headers)return app_iter ### 把值返回个调用者
这里要先介绍一个environ参数,以上方式2中涉及到了environ,其实这个environ参数是包含了请求的所有信息,让我们在看下__call__方法中, app_iter, status, headers = self.get_wsgi_response(environ)输出通过请求系列参数,获取最后要返回的get_wsgi_response,输出如下:
<werkzeug.wsgi.ClosingIterator object at 0x0589C0B0> --- 200 OK --- [('Content-Type'\\\省略]
然后在start_response(status, headers)代码中,start_response 是 application 处理完之后需要调用的函数,参数是状态码、响应头部还有错误信息,让我们来看下start_response输出,
<function WSGIRequestHandler.run_wsgi.<locals>.start_response at 0x05A32108>
跟踪代码如下start_response:
def start_response(status, response_headers, exc_info=None):if exc_info:try:if headers_sent:reraise(*exc_info)finally:exc_info = Noneelif headers_set:raise AssertionError('Headers already set')headers_set[:] = [status, response_headers]return write
start_response返回write方法,然后跟踪该方法
def write(data):assert headers_set, 'write() before start_response'if not headers_sent:status, response_headers = headers_sent[:] = headers_settry:code, msg = status.split(None, 1)except ValueError:code, msg = status, ""code = int(code)self.send_response(code, msg)header_keys = set()for key, value in response_headers:self.send_header(key, value)key = key.lower()header_keys.add(key)if not ('content-length' in header_keys or environ['REQUEST_METHOD'] == 'HEAD' or code < 200 or code in (204, 304)):self.close_connection = Trueself.send_header('Connection', 'close')if 'server' not in header_keys:self.send_header('Server', self.version_string())if 'date' not in header_keys:self.send_header('Date', self.date_time_string())self.end_headers()assert isinstance(data, bytes), 'applications must write bytes'self.wfile.write(data)self.wfile.flush()
最后就输出到浏览器,以上就是简单的请求、响应流程
Flask示例
我们在Flask中经常会写成
from flask import Flaskapp = Flask(__name__)@app.route('/index')
def index():return 'Hello World'if __name__ == '__main__':app.run() # run_simple(host,port,app)
看一下run
def run(self, host=None, port=None, debug=None, **options):from werkzeug.serving import run_simpleif host is None:host = '127.0.0.1'if port is None:server_name = self.config['SERVER_NAME']if server_name and ':' in server_name:port = int(server_name.rsplit(':', 1)[1])else:port = 5000if debug is not None:self.debug = bool(debug)options.setdefault('use_reloader', self.debug)options.setdefault('use_debugger', self.debug)try:run_simple(host, port, self, **options) ## 是不是还是用的run_simplefinally:self._got_first_request = False
最后依然是执行的run_simple(host, port, self, **options),也就是werkzeug.serving.py下的run_simple方法
####################### Flask 的 __call__ 函数 ########################
def __call__(self, environ, start_response):"""The WSGI server calls the Flask application object as theWSGI application. This calls :meth:`wsgi_app` which can bewrapped to applying middleware."""return self.wsgi_app(environ, start_response)