Talking about flask intercepting all access and before/after_request modifier, flaskafter_request

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.