from flask import Flask
from flask import abort app = Flask(__name__) # 在第一次请求之前调用,可以在此方法内部做一些初始化操作
@app.before_first_request
def before_first_request():
print("第一次请求之前调用") # 在每一次请求之前调用,这时候已经有请求了,可能在这个方法里面做请求的校验
# 如果请求的校验不成功,可以直接在此方法中进行响应,直接return之后那么就不会执行视图函数
@app.before_request
def before_request1():
print("前1") @app.before_request
def before_request2():
print("前2") @app.before_request
def before_request3():
print("前3") # 在执行完视图函数之后会调用,并且会把视图函数所生成的响应传入,可以在此方法中对响应做最后一步统一的处理
@app.after_request
def after_request1(response):
print("后1")
# response.headers["Content-Type"] = "application/json"
return response @app.after_request
def after_request2(response):
print("后2")
# response.headers["Content-Type"] = "application/json"
return response @app.after_request
def after_request3(response):
print("后3")
# response.headers["Content-Type"] = "application/json"
return response # 请每一次请求之后都会调用,会接受一个参数,参数是服务器出现的错误信息
@app.teardown_request
def teardown_request(e):
print("请每一次请求之后都会调用,会接受一个参数,参数是服务器出现的错误信息") @app.route('/')
def index():
return 'index' if __name__ == '__main__':
app.run(debug=True)

## 输出

  第一次请求之前调用
  "前1
  前2
  前3
  后3
  后2
  后1
  请每一次请求之后都会调用,会接受一个参数,参数是服务器出现的错误信息



最新文章

  1. AWS国际版的Route 53和CloudFront
  2. Jvm支持的最大线程数
  3. Ehcache(2.9.x) - API Developer Guide, Cache Event Listeners
  4. 第3组UI组件:AdapterView及其子类
  5. ViewData ViewBag ViewModel
  6. redis的常用命令
  7. ERP免费模拟上线
  8. Java中线程的使用 (2)-多线程、线程优先级、线程睡眠、让步、阻塞
  9. PGI Compiler for OpenACC Output Syntax Highlighting
  10. .net Mvc框架原理
  11. 【转】国际GNSS服务组织IGS
  12. 框架学习笔记之Mybatis(二)
  13. php过滤表单提交的html等危险代码
  14. spring-mvc访问本地html文件
  15. ORA-28000 帐户已被锁定问题处理
  16. 数独计算(C#)
  17. vue 安卓5.1 ios9 兼容性 白屏问题
  18. vSphere Data Protection – a new backup product included with vSphere 5.1
  19. zk maxClientCnxns参数
  20. python中的sort方法使用详解

热门文章

  1. 【Spring】Spring如何实现多数据源读写分离?这是我看过最详细的一篇!!
  2. Robot Framework(1)——环境搭建及安装
  3. 国际象棋棋盘输出-PHP代码
  4. Inherent Adversarial Robustness of Deep Spiking Neural Networks: Effects of Discrete Input Encoding and Non-Linear Activations
  5. 牛客网PAT练兵场-月饼
  6. JS手写call、bind、apply
  7. 从String类型发散想到的一些东西
  8. 【Pytorch-入门】windows下的环境搭建(经验证成功~)
  9. 用Python写一个随机数字生成代码,5行代码超简单
  10. Hint usenl usage /*+ leading(emp,dept) usenl(emp) */