Python (Web era) - request hook

Introduction

Sometimes it is necessary to execute part of the code before or after processing the request, such as creating a database link or authenticating login permissions, etc., specifying the interactive format of the data at the end of the request, etc. In order to avoid writing repeated code in each view function, flask provides the function of registering universal functions (request hook) .

The registered function can be called before or after the request is dispatched to the view function, the request hook is passed Decorator formimplementation, Flask supports the followingfour request hooks:

  • before_first_request: Run before the first request is processed

  • before_request: Run before each request

  • after_request: Run after each request

    • Accepts parameters: response object of the view function

    • The response object in the parameter needs to be returned in this method

  • teardown_request: runs after each request

    • Accepts parameters: error message object

code example

# 设置访问路径,项目根路径将访问以下index方法
@app.route("/")
def index():
    return "hello world!!!!"


@app.route("/test")
def test():
    a=1/0
    return "hello world test!!!!"


@app.before_first_request
def handle_before_first_request():
    """
    在第一次请求之前执行
    """
    print("handle_before_first_request..............")


@app.before_request
def handle_before_request():
    """
    在每次请求前执行
    """
    print("handle_before_request..........")


@app.after_request
def handle_after_request(response):
    """
    在每次请求处理之后执行 ,如果有异常则不执行
    """
    print("handle_after_request....")
    print(response)
    return response

@app.teardown_request
def handle_teardown_request(error):
    """
    在每次请求处理之后执行,不管是否有异常都执行
    :param error:
    :return:
    """
    print("handle_teardown_request.....")
    print(error)
wAAACH5BAEKAAAAAAAAAABAAEAAAICRAEAOw==
 
 

Call index interface

picture

The console output is as follows:

picture

ContinueCall the test interface (there is a divide-by-0 exception in the interface)

picture

The console output is as follows:

picture

in conclusion

  • @app.before_first_request: It will only be executed once before the first call to the view function, and will not be executed if called after that.

  • @app.before_request: Will be executed every time the view function is called

  • @app.after_request: It will be executed every time the view function is called. The response object must be returned. If an exception occurs, it will be receivedGo to the server exception object with status code 500, which is the built-in exception result of flask

  • @app.teardown_request:It will be executed every time the view function is called, no need to returnresponse Object,If an exception occurs, can receive specific exception information object

The Record of Programmers and Investment Life has been renamed Programmer Zhiqiu, which is the same as the WX official account. Welcome to pay attention! !

Guess you like

Origin blog.csdn.net/qq_25702235/article/details/132151583