flask request usage hook

Request hook

The client and server interaction process, some preparatory work or finishing touches need to be addressed, such as:

  • When the start request, establish a database connection;
  • When the start request, checking permissions on demand;
  • At the end of the request, the specified data interchange format;

To give every view function to avoid duplicate coding function, Flask provides the functionality common facilities, that request hooks.

Hook request is implemented in the form of decorators, Flask support hooks following four request:

  • before_first_request
    • Executed before processing the first request
  • before_request
    • Performed prior to each request
    • If it returns a response of a modified function, the function will not be called view
  • after_request
    • If no error is thrown after each execution request
    • Accepts a parameter: in response to the view function
    • In this function, the response value may be the last step before returning modification process
    • You need to be returned in response to parameters in this parameter
  • teardown_request:
    • After each execution request
    • Takes one argument: the error message, if there is an error thrown related

Code Testing

from flask import Flask
from flask import abort

app = Flask(__name__)


# 在第一次请求之前调用,可以在此方法内部做一些初始化操作
@app.before_first_request
def before_first_request():
    print("before_first_request")


# 在每一次请求之前调用,这时候已经有请求了,可能在这个方法里面做请求的校验
# 如果请求的校验不成功,可以直接在此方法中进行响应,直接return之后那么就不会执行视图函数
@app.before_request
def before_request():
    print("before_request")
    # if 请求不符合条件:
    #     return "laowang"


# 在执行完视图函数之后会调用,并且会把视图函数所生成的响应传入,可以在此方法中对响应做最后一步统一的处理
@app.after_request
def after_request(response):
    print("after_request")
    response.headers["Content-Type"] = "application/json"
    return response


# 请每一次请求之后都会调用,会接受一个参数,参数是服务器出现的错误信息
@app.teardown_request
def teardown_request(e):
    print("teardown_request")


@app.route('/')
def index():
    return 'index'

if __name__ == '__main__':
    app.run(debug=True)
  • At the request of the 1st printing:
before_first_request
before_request
after_request
teardown_request
  • When a print request 2nd:
before_request
after_request
teardown_request

Guess you like

Origin blog.csdn.net/qwertyuiopasdfgg/article/details/93332863