[06] Flask tutorial requests 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 the Flask Import the Flask
 from the Flask Import ABORT 

App = the Flask ( __name__ ) 


# before the first call request, you can do some initialization operations inside this method 
@ app.before_first_request
 DEF before_first_request ():
     Print ( " before_first_request " ) 


# in each Called before the first request, this time has been requested, may make a request in which this method check 
# If the requested verification fails, you can respond directly to this method, then it will not return after direct view function execution 
app.before_request @
 DEF before_request ():
     Print ( " before_request " )
     # IF request does not meet the conditions:
    #      Return "laowang" 


# After performing the function calls the view, and the view will function generated in response to afferent, this method can make the final step in response to uniform treatment 
@ app.after_request
 DEF after_request (the Response):
     Print ( " after_request " ) 
    Response.Headers [ " Content-Type " ] = " the Application / json " 
    return the Response 


# please each will be called after a request, it takes one parameter, the parameter is a server error message appears 
@ app.teardown_request
 DEF teardown_request (E):
     Print ( " teardown_request " ) 


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

IF  the __name__ == ' __main__ ' : 
    app.run (Debug = True) 

------------------------- -------------------------------------------------- -------------------------------------------------- ------- printed on the 1st request: before_first_request before_request after_request teardown_request
at the time of the second print request: before_request after_request teardown_request

 

Guess you like

Origin www.cnblogs.com/zeug/p/11363958.html