Get the App
SLTechnology News&Howtos  ›  Development  › 

Flask intercepts all visits and example Analysis of before/after_request modifier

Shulou Source: shulou.com Published: 2022-06-03 16:46:49 09月16日 Update

This article mainly shows you the "flask intercepts all visits and before/after_request modifier example analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "flask intercepts all visits and before/after_request modifier sample analysis" this article.

The details are as follows.

When learning to develop the acquired Android interface with flask, there is a requirement to intercept all requests, that is, all requests are processed before entering the app.route decorated function.

After searching for information on the Internet, I know the two methods @ before_request and @ after_request. Examples:

@ app.before_request def before_request (): ip = request.remote_addr url = request.url print ip, print url

After the before_request () function is modified by app.before_request, after each request arrives, it will first enter into the function before_request (), such as the above code, get the ip and url of the request, and print it out. After execution, the request will normally enter the response in the app.route-modified function. If multiple functions are modified by app.before_request, these functions will be executed in turn.

App.before_request modifiers are very useful in development, such as determining whether an ip has malicious access behavior to intercept and so on.

In addition, by the same token, the app.after_request modifier is executed after the user requests a function response, but it should be noted that this execution is called before the function returns data, that is, the request has been responded to by the function modified by app.route, and the response has been formed, but has not yet been returned to the user.

The above is all the content of the article "flask intercepts all visits and sample analysis of before/after_request modifiers". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Functions examples analysis content articles learning is in the user help development two code multiple as above that is malicious interface data method time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Tech Info Shulou Information MySQL Linux