Flask中一次请求到响应的流程
admin
2023-07-31 00:45:15
0

1、首先根据WSGI发送的environ变量获取请求上下文,主要是根据函数ctx = self.request_context(environ),然后将该请求上下文推入全局变量_request_ctx_stack中(ctx.push()).

12 def request_context(self, environ):    return RequestContext(self, environ)



2、得到请求后,要触发第一次请求函数,如果是该应用是第一次实例化,并存在第一次请求之前函数(存在before_first_req_func字典中中),会调用存在该字典中的函数。当然一个实例也只执行一次,即在初始化的时候执行。

3、会发送请求开始信号,request_started,告知subscriber请求开始了。

4、如果存在before_request装饰的函数(函数位置在before_request_func字典中),那么在调用正常请求前会调用该函数。

12345678910 @setupmethoddef before_request(self, f):    \”\”\”Registers a function to run before each request.The function will be called without any arguments.If the function returns a non-None value, it\’s handled asif it was the return value from the view and furtherrequest handling is stopped.\”\”\”self.before_request_funcs.setdefault(None, []).append(f)return f

从函数可以看到,这是一个装饰器,被该装饰器修饰的函数会添加到字典中。

5、调用正常的请求,返回一个该请求函数的值。 调用请求的源代码:

12345678910111213141516 def dispatch_request(self):    \”\”\”Does the request dispatching.  Matches the URL and returns the    return value of the view or error handler.  This does not have to    be a response object.  In order to convert the return value to a    proper response object, call :func:make_response.req = _request_ctx_stack.top.requestif req.routing_exception is not None:    self.raise_routing_exception(req)rule = req.url_rule# if we provide automatic options for this URL and the# request came with the OPTIONS method, reply automaticallyif getattr(rule, \’provide_automatic_options\’, False)    and req.method == \’OPTIONS\’:    return self.make_default_options_response()# otherwise dispatch to the handler for that endpointreturn self.view_functions[rule.endpoint](**req.view_args)

6、将请求函数返回值构造成响应类。

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 def make_response(self, rv):    \”\”\”Converts the return value from a view function to a real    response object that is an instance of :attr:response_class.The following types are allowed for `rv`:  ======================= ===========================================:attr:`response_class`  the object is returned unchanged:class:`str`            a response object is created with the                        string as body:class:`unicode`        a response object is created with thequest_ctx_stack中(ctx.push()).

12 def request_context(self, environ):    return RequestContext(self, environ)



2、得到请求后,要触发第一次请求函数,如果是该应用是第一次实例化,并存在第一次请求之前函数(存在before_first_req_func字典中中),会调用存在该字典中的函数。当然一个实例也只执行一次,即在初始化的时候执行。

3、会发送请求开始信号,request_started,告知subscriber请求开始了。

4、如果存在before_request装饰的函数(函数位置在before_request_func字典中),那么在调用正常请求前会调用该函数。

12345678910 @setupmethoddef before_request(self, f):    \”\”\”Registers a function to run before each request.The function will be called without any arguments.If the function returns a non-None value, it\’s handled asif it was the return value from the view and furtherrequest handling is stopped.\”\”\”self.before_request_funcs.setdefault(None, []).append(f)return f

从函数可以看到,这是一个装饰器,被该装饰器修饰的函数会添加到字典中。

5、调用正常的请求,返回一个该请求函数的值。 调用请求的源代码:

12345678910111213141516 def dispatch_request(self):    \”\”\”Does the request dispatching.  Matches the URL and returns the    return value of the view or error handler.  This does not have to    be a response object.  In order to convert the return value to a    proper response object, call :func:make_response.req = _request_ctx_stack.top.requestif req.routing_exception is not None:    self.raise_routing_exception(req)rule = req.url_rule# if we provide automatic options for this URL and the# request came with the OPTIONS method, reply automaticallyif getattr(rule, \’provide_automatic_options\’, False)    and req.method == \’OPTIONS\’:    return self.make_default_options_response()# otherwise dispatch to the handler for that endpointreturn self.view_functions[rule.endpoint](**req.view_args)

6、将请求函数返回值构造成响应类。

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 def make_response(self, rv):    \”\”\”Converts the return value from a view function to a real    response object that is an instance of :attr:response_class.The following types are allowed for `rv`:  ======================= ===========================================:attr:`response_class`  the object is returned unchanged:class:`str`            a response object is created with the                        string as body:class:`unicode`        a response object is created with the

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
python清除字符串里非数字... 本文实例讲述了python清除字符串里非数字字符的方法。分享给大家供大家参考。具体如下: impor...