08 Flask源码剖析之flask拓展点

1. 信号(源码)

  • 信号,是在flask框架中为我们预留的钩子,让我们可以进行一些自定义操作。

    pip3 install blinker

2. 根据flask项目的请求流程来进行设置扩展点

  • 中间件

    # 代码示例
    
    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()
  • 当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__
  • 执行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()
  • 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()
  • 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()
  • 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()
  • 视图函数

  • 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()
  • 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()
  • 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()
  • 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()
  • 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()
  • 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()
  • 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()
  • 扩展:flash

    flash存值之后只能取一次

    # 代码示例
    
    from flask import Flask,render_template,flash,get_flashed_messages,session
    from flask import signals
    app = Flask(__name__)
    app.secret_key = 'iuknsoiuwknlskjdf' @app.route('/index/')
    def index():
    # flash('123')
    session['k1'] = 123
    return render_template('index.html') @app.route('/order')
    def order():
    # messages = get_flashed_messages()
    # print(messages)
    val = session['k1']
    del session['k1']
    print(val)
    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")

08 Flask源码剖析之flask拓展点的更多相关文章

  1. 02 flask源码剖析之flask快速使用

    02 flask快速使用 目录 02 flask快速使用 1.flask与django的区别 2. 安装 3. 依赖wsgi Werkzeug 4. 快速使用flask 5. 用户登录&用户管 ...

  2. flask源码剖析系列(系列目录)

    flask源码剖析系列(系列目录) 01 flask源码剖析之werkzurg 了解wsgi 02 flask源码剖析之flask快速使用 03 flask源码剖析之threading.local和高 ...

  3. flask 源码剖析

    flask 上下文管理源码流程及涉及的部分技术点 [flask源码梳理]之一  偏函数_mro [flask源码梳理]之二  面向对象中__setattr__ [flask源码梳理]之三  Local ...

  4. 08 jwt源码剖析

    08 jwt源码剖析 目录 08 jwt源码剖析 1. jwt认证流程 2.jwt创建token 2.1 原理 2.2 jwt校验token 3. jwt使用 4. 源码剖析 总结: JSON Web ...

  5. Flask源码剖析详解

    1. 前言 本文将基于flask 0.1版本(git checkout 8605cc3)来分析flask的实现,试图理清flask中的一些概念,加深读者对flask的理解,提高对flask的认识.从而 ...

  6. 04 flask源码剖析之LocalStack和Local对象实现栈的管理

    04 LocalStack和Local对象实现栈的管理 目录 04 LocalStack和Local对象实现栈的管理 1.源码入口 1. flask源码关于local的实现 2. flask源码关于l ...

  7. 05 flask源码剖析之配置加载

    05 Flask源码之:配置加载 目录 05 Flask源码之:配置加载 1.加载配置文件 2.app.config源码分析 3.from_object源码分析 4. 总结 1.加载配置文件 from ...

  8. 06 flask源码剖析之路由加载

    06 Flask源码之:路由加载 目录 06 Flask源码之:路由加载 1.示例代码 2.路由加载源码分析 1.示例代码 from flask import Flask app = Flask(__ ...

  9. 07 flask源码剖析之用户请求过来流程

    07 Flask源码之:用户请求过来流程 目录 07 Flask源码之:用户请求过来流程 1.创建ctx = RequestContext对象 2. 创建app_ctx = AppContext对象 ...

随机推荐

  1. 如何在react中使用decorator

    2020-03-27 如何在react中使用decorator decorator目前都需要修改babel才能使用 说一下具体的操作方法 踩了一天的坑... 步骤1: yarn create reac ...

  2. Nirvana【思维+暴力优化】

    Nirvana 题目链接(点击) Kurt reaches nirvana when he finds the product of all the digits of some positive i ...

  3. Oracle VM VirtualBox 连接 Centos7 minimal版

    概述: 本博客是系列博客,主要讲述在Windows环境下安装虚拟机,在虚拟机中安装lunix系统,在lunix下安装docker,在docker中安装并使用常用的开发软件,比如tomcat.redis ...

  4. Oracle调优之看懂Oracle执行计划

    @ 目录 1.文章写作前言简介 2.什么是执行计划? 3.怎么查看执行计划? 4.查看真实执行计划 5.看懂Oracle执行计划 5.1 查看explain 5.2 explain执行顺序 5.3 访 ...

  5. vc6.0的项目如何在整个项目中查询内容呢?试试vs2015

    vc6.0的项目如何在整个项目中查询内容呢?试试vs2015 https://blog.csdn.net/txwtech/article/details/101308795

  6. 查找nginx安装目录并启动

    今天公司突然停电,来电后发现服务无法访问了,服务器是部署在公司内 发现ip ping 不通,是服务器没开 手动开了服务器,还是无法访问 可以FTP,但是不能访问服务,说明机器已经开了,有些东西应该没启 ...

  7. Hystrix Stream的监控页面不显示内容

    打开Hystrix Stream页面,进入后,发现只有一行Unable to connect to Command Metric Stream. 因为springboot的默认路径不是 "/ ...

  8. 02【熟悉】springboot和微服务的介绍

    1,springboot简介 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程. 该框架使用了特定的方式来进行配置,从 ...

  9. web安全之跨站脚本漏洞(XSS)

    XSS(跨站脚本)概述以及pikachu上的实验操作 Cross-Site Scripting 简称为“CSS”,为避免与前端叠成样式表的缩写"CSS"冲突,故又称XSS. XSS ...

  10. shell把字符串中的字母去掉,只保留数字

    1 编辑测试文件 [root@hz-kvm cephdisk3]# cat > 1.txt <<EOF> 120Tib> EOF 2 显示文件[root@hz-kvm c ...