Talking about flask intercepting all access and before/after_request modifier, flaskafter_request
This article mainly studies how flask intercepts all access and the related content of before_request and after_request modifiers.
When I learned how to use flask to develop the android interface the day after tomorrow, I met a requirement that I wanted to intercept all requests, that is, all requests were processed before they entered the function decorated by app. route.
After searching for information on the Internet, you can find the @ before_request and @ after_request methods, for example:
@ App. before_request def before_request (): ip = request. remote_addr url = request. url print ip, print url
The before_request () function is called by the app. after before_request is modified, after each request arrives, it will first enter the before_request () function. The code above gets the request ip address and url and prints it out, after the execution is complete, the request enters the app normally. response in the route-modified function. If multiple functions are called by the app. if before_request is modified, these functions are executed in sequence.
The app. before_request modifier is very useful in development. For example, it can be used to determine whether an ip address has malicious access behavior and intercept it.
In addition, the app. the after_request modifier is executed after the user request obtains the function response. However, it must be noted that the modifier is called before the function returns data, that is, the request has been applied. the route-modified function has responded and has formed a response, but it is called before it is returned to the user.
Summary
The above section describes how flask intercepts all access requests and all content of the before/after_request modifier. I hope this will help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!