当前位置:网站首页>Analysis of flask source code (I) request entry
Analysis of flask source code (I) request entry
2022-07-23 11:10:00 【Purple green sword】
Flask Source analysis
flask It's based on Python Developed wsgi Micro frame .flask There are two core dependency Libraries :Werkzug and jinjia. among werkzeug Responsible for the core logic module , Like routing 、 Encapsulation of requests and responses 、WSGI Related functions, etc ;jinja Responsible for rendering templates , It's mainly used to render the html The contents of the document .
1. entrance
web Programs are from the server through related wsgi In the relevant program of conversion ,Django This method has been used in ,Django 3.x And later versions have added ASGI Processing requests asynchronously .
flask What's used internally in is Werkzug Processing performed by the module .
1.1 Werkzug Introduction and use of
werkzueg Is an excellent tool library , Use werkzueg To achieve a fit WSGI Agreed web Applications
Supplement of object-oriented knowledge :
class Base:
def __call__(self, *args, **kwargs):
print("call Yes ")
def func(self):
print("func ...")
obj = Base()
# obj()
obj.func()
Easy to use
from werkzeug import run_simple
def func(environ,start_response):
# To change the function, you need to write these two parameters , Because these two values are needed inside the source code
print(" The request came. ")
if __name__ == '__main__':
# app.run()
run_simple("127.0.0.1",5000,func)
>>> Click request address ( When the request comes in )
>>> The request came.
>>> Report errors
# The function is executed as func()
From the above example, we can see , When you click request address ( When the request comes in ), adopt werkzug Started a small web Program , And execute related functions . When we try to change functions into classes .
from werkzeug import run_simple
class FlaskBase:
def __call__(self, *args, **kwargs):
print("call Method executed ")
app = FlaskBase()
if __name__ == '__main__':
# app.run()
run_simple("127.0.0.1",5000,app)
>>> Click request address
>>> call The method is implemented
>> Report errors
Particular attention : When you click request address ( When the request comes in ), Through this, you can find that the object is passed into , It's equivalent to executing object (), Therefore, the incoming and outgoing values go back to the corresponding implementation class __call__ Method .
werkzug start-up flask
from flask import Flask
from werkzeug import run_simple
app = Flask(__name__)
if __name__ == '__main__':
run_simple("127.0.0.1",5000,app)
- werkzug You can put a small flask The application starts directly .
1.2 ordinary Flask Website
# The simplest flask application
from flask import Flask
app = Flask(__name__)
if __name__ == '__main__':
app.run()
flask During the startup of run() Method .
# Flask Internal source code :run Part of the source code inside the method
from werkzeug.serving import run_simple
try:
run_simple(t.cast(str, host), port, self, **options)
# There are mainly three parameters passed here : Address 、 Port number 、 object .
# In particular : The object here is an object that has been instantiated outside , namely app
# According to the above research , When the request comes in , Will execute flask Of __call__ Method .
finally:
# reset the first request information if the development server
# reset normally. This makes it possible to restart the server
# without reloader and that stuff from an interactive shell.
self._got_first_request = False
flask Inside __call__() Method
def __call__(self, environ: dict, start_response: t.Callable) -> t.Any:
"""The WSGI server calls the Flask application object as the WSGI application. This calls :meth:`wsgi_app`, which can be wrapped to apply middleware. """
# Here you will see that the function also has these two parameters environ,start_response
return self.wsgi_app(environ, start_response)
- Through the internal method of the source code, we can see ,Flask The essence of startup is still
Werkzug.run_simple()function , Conduct web The start of the program .
Conclusion :
- flask from werkzug To start the , In essence, what is carried out is
Werkzug.run_simple()function . - important :flask in , When the request comes , The execution is object
__call__()Method
Although the rain is wide , Not moist and rootless . Learning is difficult , Diligence can reach its peak ;
边栏推荐
- sprintboot中使用缓存时候,数据加载不出来
- Heidelberg CP2000 circuit board maintenance printer host controller operation and maintenance precautions
- JDBC Learning and simple Encapsulation
- JDBC数据库连接池
- 【无标题】
- 安装企业版pycharm以及anaconda
- 防止神经网络过拟合的五种方法
- Partial usage of C #
- An accident caused by MySQL misoperation, and "high availability" is not working well
- Xssgame games (XSS learning) level1-15
猜你喜欢
随机推荐
3.Flask 中的线程
框架介绍mvt
达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程
【无标题】
Fundamentals of software testing - design method of test cases
Flask源码剖析(一)请求入口
【无标题】
Mysql database foundation
9. Ray tracing
Pyqt5 use qpainter to draw the coordinate axis and display the scatter diagram
【无标题】
adb常用命令
cuda10.0配置pytorch1.7.0+monai0.9.0
JDBC Learning and simple Encapsulation
十年架构五年生活-01毕业之初
p5面试题
img标签设置height和width无效
知识点回顾
人脸识别神经网络实现
Anr error encountered









