Flask扩展点总结(信号)
信号(源码)
信号,是在flask框架中为我们预留的钩子,让我们可以进行一些自定义操作。
pip3 install blinker
根据flask项目的请求流程来进行设置扩展点
1.中间件
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/index')
def index():
return render_template('index.html')
@app.route('/order')
def order():
return render_template('order.html')
class MyMiddleware(object):
def __init__(self,old_app):
self.wsgi_app = old_app.wsgi_app
def __call__(self, *args, **kwargs):
print('123')
result = self.wsgi_app(*args, **kwargs)
print('456')
return result
app.wsgi_app = MyMiddleware(app)
if __name__ == '__main__':
app.run()
2.当app_ctx被push到local中栈之后,会触发appcontext_pushed信号,之前注册在这个信号中的方法,就会被执行。
from flask import Flask,render_template
from flask import signals
app = Flask(__name__)
@signals.appcontext_pushed.connect
def f1(arg):
print('appcontext_pushed信号f1被触发',arg)
@signals.appcontext_pushed.connect
def f2(arg):
print('appcontext_pushed信号f2被触发',arg)
@app.route('/index')
def index():
return render_template('index.html')
@app.route('/order')
def order():
return render_template('order.html')
if __name__ == '__main__':
app.run()
# app.__call__
3.执行before_first_request扩展
from flask import Flask,render_template
app = Flask(__name__)
@app.before_first_request
def f2():
print('before_first_requestf2被触发')
@app.route('/index')
def index():
return render_template('index.html')
@app.route('/order')
def order():
return render_template('order.html')
if __name__ == '__main__':
app.run()
4.request_started信号
from flask import Flask,render_template
from flask import signals
app = Flask(__name__)
@signals.request_started.connect
def f3(arg):
print('request_started信号被触发',arg)
@app.route('/index')
def index():
return render_template('index.html')
@app.route('/order')
def order():
return render_template('order.html')
if __name__ == '__main__':
app.run()
5.url_value_processor
from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__)
@app.url_value_preprocessor
def f5(endpoint,args):
print('f5')
@app.route('/index/')
def index():
print('index')
return render_template('index.html')
@app.route('/order')
def order():
print('order')
return render_template('order.html')
if __name__ == '__main__':
app.run()
6.before_reuqest
from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__)
@app.before_request
def f6():
g.xx = 123
print('f6')
@app.route('/index/')
def index():
print('index')
return render_template('index.html')
@app.route('/order')
def order():
print('order')
return render_template('order.html')
if __name__ == '__main__':
app.run()
视图函数
7.before_render_template / rendered_template
from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__)
@signals.before_render_template.connect
def f7(app, template, context):
print('f7')
@signals.template_rendered.connect
def f8(app, template, context):
print('f8')
@app.route('/index/')
def index():
return render_template('index.html')
@app.route('/order')
def order():
print('order')
return render_template('order.html')
if __name__ == '__main__':
app.run()
8.after_request
from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__)
@app.after_request
def f9(response):
print('f9')
return response
@app.route('/index/')
def index():
return render_template('index.html')
@app.route('/order')
def order():
print('order')
return render_template('order.html')
if __name__ == '__main__':
app.run()
9.request_finished
from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__)
@signals.request_finished.connect
def f10(app,response):
print('f10')
@app.route('/index/')
def index():
return render_template('index.html')
@app.route('/order')
def order():
print('order')
return render_template('order.html')
if __name__ == '__main__':
app.run()
10.got_request_exception
from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__)
@app.before_first_request
def test():
int('asdf')
@signals.got_request_exception.connect
def f11(app,exception):
print('f11')
@app.route('/index/')
def index():
return render_template('index.html')
@app.route('/order')
def order():
print('order')
return render_template('order.html')
if __name__ == '__main__':
app.run()
11.teardown_request
from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__)
@app.teardown_request
def f12(exc):
print('f12')
@app.route('/index/')
def index():
return render_template('index.html')
@app.route('/order')
def order():
print('order')
return render_template('order.html')
if __name__ == '__main__':
app.run()
12.request_tearing_down
from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__)
@signals.request_tearing_down.connect
def f13(app,exc):
print('f13')
@app.route('/index/')
def index():
return render_template('index.html')
@app.route('/order')
def order():
print('order')
return render_template('order.html')
if __name__ == '__main__':
app.run()
13.appcontext_popped
from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__)
@signals.appcontext_popped.connect
def f14(app):
print('f14')
@app.route('/index/')
def index():
return render_template('index.html')
@app.route('/order')
def order():
print('order')
return render_template('order.html')
if __name__ == '__main__':
app.run()
总结:关于flask内部共有14+个扩展点用于我们对flask框架内部进行定制,其中有:9个是信号。
template_rendered = _signals.signal("template-rendered")
before_render_template = _signals.signal("before-render-template")
request_started = _signals.signal("request-started")
request_finished = _signals.signal("request-finished")
request_tearing_down = _signals.signal("request-tearing-down")
got_request_exception = _signals.signal("got-request-exception")
appcontext_tearing_down = _signals.signal("appcontext-tearing-down")
appcontext_pushed = _signals.signal("appcontext-pushed")
appcontext_popped = _signals.signal("appcontext-popped")
message_flashed = _signals.signal("message-flashed")
Flask扩展点总结(信号)的更多相关文章
- Inside Flask - flask 扩展加载过程
Inside Flask - flask 扩展加载过程 flask 扩展(插件)通常是以 flask_<扩展名字> 为扩展的 python 包名,而使用时,可用 import flask. ...
- Flask從入門到入土(二)——請求响应與Flask扩展
———————————————————————————————————————————————————————————— 一.程序和請求上下文 Flask從客戶端收到請求時,要讓視圖函數能訪問一些對象 ...
- Flask 扩展 自定义扩展
创建一个为视图访问加日志的扩展Flask-Logging,并从中了解到写Flask扩展的规范. 创建工程 先创建一个工程,目录结构如下: flask-logging/ ├ LICENSE # 授权说明 ...
- 补充的flask实例化参数以及信号
一.实例化补充 instance_path和instance_relative_config是配合来用的.这两个参数是用来找配置文件的,当用app.config.from_pyfile('settin ...
- Flask从入门到精通之flask扩展
Flask被设计成可扩展形式,因此并没有提供一些重要的功能,比如数据库和用户认证,所以开发者可以自由选择最适合程序的包,或者按需求自行开发.社区成员开发了大量不同用途的扩展,如果这还不能满足需求,你还 ...
- Flask系列(六)Flask实例化补充及信号
一.实例化补充 instance_path和instance_relative_config是配合来用的. 这两个参数是用来找配置文件的,当用app.config.from_pyfile('setti ...
- flask实例化参数以及信号
一.实例化补充 instance_path和instance_relative_config是配合来用的.这两个参数是用来找配置文件的,当用app.config.from_pyfile('settin ...
- Flask扩展实现HTTP令牌token认证HTTPTokenAuth
Token认证 在restful设计中,用户认证模式通常使用json web token,而不会使用传统的HTTP Basic认证(传入账号密码) token认证模式如下:在请求header中加入to ...
- 2.6、Flask扩展
Flask 被设计为可扩展形式,故而没有提供一些重要的功能,例如数据库和用户认证,所以开发者可以自由选择最适合程序的包,或者按需求自行开发. 社区成员开发了大量不同用途的扩展,如果这还不能满足需求,你 ...
随机推荐
- SpringBoot进阶教程(六十七)RateLimiter限流
在上一篇文章nginx限流配置中,我们介绍了如何使用nginx限流,这篇文章介绍另外一种限流方式---RateLimiter. v限流背景 在早期的计算机领域,限流技术(time limiting)被 ...
- 顶会两篇论文连发,华为云医疗AI低调中崭露头角
摘要:2020年国际医学图像计算和计算机辅助干预会议(MICCAI 2020),论文接收结果已经公布.华为云医疗AI团队和华中科技大学合作的2篇研究成果入选. 同时两篇研究成果被行业顶会收录,华为云医 ...
- 安装篇六:安装PHP(7.2.29版本)
准备环境,下载依赖软件 # No1:在前面安装好的基础上,关闭iptables.selinux # No2:安装依赖包 yum install zlib-devel bzip2-devel -y &l ...
- 设计模式之-Builder模式
场景引入: 一个类,如果有多个属性时,在创建对象,如何对属性进行赋值呢? 1.通过构造器赋值,这种方案优点时一次性赋值完成,但是多种属性的组合,导致构造器会非常多. 2.通过setter方法赋值,方案 ...
- CommandLineRunner和ApplicationRunner
使用场景 我们在开发过程中会有这样的场景:需要在容器启动的时候执行一些内容,比如:读取配置文件信息,数据库连接,删除临时文件,清除缓存信息,在Spring框架下是通过ApplicationListen ...
- java动态代理实现与原理详细分析(转)
关于Java中的动态代理,我们首先需要了解的是一种常用的设计模式--代理模式,而对于代理,根据创建代理类的时间点,又可以分为静态代理和动态代理. 一.代理模式 代理模式是常用的java设计模式, ...
- Java虚拟机详解04----GC算法和种类
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- 配置 nginx 访问资源目录,nginx配置 root 与 alias 的区别
比如说想要把 /home/source 目录作为资源目录,那么需要如下配置: location /source/ { #识别url路径后,nginx会到/home/文件路径下,去匹配/source r ...
- Spark推荐系统实践
推荐系统是根据用户的行为.兴趣等特征,将用户感兴趣的信息.产品等推荐给用户的系统,它的出现主要是为了解决信息过载和用户无明确需求的问题,根据划分标准的不同,又分很多种类别: 根据目标用户的不同,可划分 ...
- 【Redis3.0.x】NoSql 入门
Redis3.0.x NoSql 入门 概述 NoSQL(Not Only SQL ),即不仅仅是 SQL,泛指非关系型的数据库.NoSQL 数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑 ...