国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Flask之請求鉤子的實現

2020-01-04 13:44:08
字體:
來源:轉載
供稿:網友

請求鉤子

通過裝飾器為一個模塊添加請求鉤子, 對當前模塊的請求進行額外的處理. 比如權限驗證.

說白了,就是在執行視圖函數前后你可以進行一些處理,Flask使用裝飾器為我們提供了注冊通用函數的功能。

1、before_first_request:在處理第一個請求前執行

before_first_request

在對應用程序實例的第一個請求之前注冊要運行的函數, 只會執行一次

  #: A lists of functions that should be called at the beginning of the  #: first request to this instance. To register a function here, use  #: the :meth:`before_first_request` decorator.  #:  #: .. versionadded:: 0.8  self.before_first_request_funcs = []  @setupmethod  def before_first_request(self, f):    """Registers a function to be run before the first request to this    instance of the application.    .. versionadded:: 0.8    """    self.before_first_request_funcs.append(f) 

將要運行的函數存放到before_first_request_funcs 屬性中進行保存

2、before_request:在每次請求前執行

在每個請求之前注冊一個要運行的函數, 每一次請求都會執行

  #: A dictionary with lists of functions that should be called at the  #: beginning of the request. The key of the dictionary is the name of  #: the blueprint this function is active for, `None` for all requests.  #: This can for example be used to open database connections or  #: getting hold of the currently logged in user. To register a  #: function here, use the :meth:`before_request` decorator.  self.before_request_funcs = {}   @setupmethod  def before_request(self, f):    """Registers a function to run before each request."""    self.before_request_funcs.setdefault(None, []).append(f)    return f

將要運行的函數存放在字典中, None 為鍵的列表中存放的是整個應用的所有請求都要運行的函數.

3、after_request:每次請求之后調用,前提是沒有未處理的異常拋出

在每個請求之后注冊一個要運行的函數, 每次請求都會執行. 需要接收一個 Response 類的對象作為參數 并返回一個新的Response 對象 或者 直接返回接受到的Response 對象

  #: A dictionary with lists of functions that should be called after  #: each request. The key of the dictionary is the name of the blueprint  #: this function is active for, `None` for all requests. This can for  #: example be used to open database connections or getting hold of the  #: currently logged in user. To register a function here, use the  #: :meth:`after_request` decorator.  self.after_request_funcs = {}  @setupmethod  def after_request(self, f):    """Register a function to be run after each request. Your function    must take one parameter, a :attr:`response_class` object and return    a new response object or the same (see :meth:`process_response`).    As of Flask 0.7 this function might not be executed at the end of the    request in case an unhandled exception occurred.    """    self.after_request_funcs.setdefault(None, []).append(f)    return f

4、teardown_request:每次請求之后調用,即使有未處理的異常拋出

注冊一個函數在每個請求的末尾運行,不管是否有異常, 每次請求的最后都會執行.

  #: A dictionary with lists of functions that are called after  #: each request, even if an exception has occurred. The key of the  #: dictionary is the name of the blueprint this function is active for,  #: `None` for all requests. These functions are not allowed to modify  #: the request, and their return values are ignored. If an exception  #: occurred while processing the request, it gets passed to each  #: teardown_request function. To register a function here, use the  #: :meth:`teardown_request` decorator.  #:  #: .. versionadded:: 0.7  self.teardown_request_funcs = {}  @setupmethod  def teardown_request(self, f):    """Register a function to be run at the end of each request,    regardless of whether there was an exception or not. These functions    are executed when the request context is popped, even if not an    actual request was performed.    """    self.teardown_request_funcs.setdefault(None, []).append(f)    return f

將要運行的函數存放在字典中, None 為鍵的列表中存放的是整個應用的所有請求都要運行的函數.

from flask import Flaskapp = Flask(__name__)@app.before_first_requestdef before_first_request():  print('before_first_request')@app.before_requestdef before_request():  print('before_request')@app.after_requestdef after_request(resp):  print('after_request')  return resp@app.teardown_requestdef teardown_request(e):  print('teardown_request')@app.route("/")def view_fn():  return "view_fn"  if __name__ == "__main__":  app.run()

第一次請求:

頁面輸出:view_fn
控制臺輸出: before_first_request
            before_request
            after_request
            teardown_request

第二次請求:

頁面輸出:view_fn
控制臺輸出: before_request
            after_request
            teardown_request

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳新县| 监利县| 水城县| 顺昌县| 宣威市| 会昌县| 儋州市| 沧州市| 盐边县| 香河县| 资兴市| 曲沃县| 天全县| 鄄城县| 琼海市| 竹山县| 宕昌县| 监利县| 南雄市| 竹北市| 新田县| 赣州市| 天台县| 清丰县| 合山市| 攀枝花市| 平湖市| 贵德县| 乐安县| 西乡县| 垣曲县| 偏关县| 土默特右旗| 高平市| 新竹市| 大丰市| 东乡族自治县| 民乐县| 穆棱市| 涪陵区| 盐亭县|