最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Flask之請(qǐng)求鉤子的實(shí)現(xiàn)

 更新時(shí)間:2018年12月23日 10:21:31   作者:二十一  
這篇文章主要介紹了Flask之請(qǐng)求鉤子的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

請(qǐng)求鉤子

通過裝飾器為一個(gè)模塊添加請(qǐng)求鉤子, 對(duì)當(dāng)前模塊的請(qǐng)求進(jìn)行額外的處理. 比如權(quán)限驗(yàn)證.

說白了,就是在執(zhí)行視圖函數(shù)前后你可以進(jìn)行一些處理,F(xiàn)lask使用裝飾器為我們提供了注冊(cè)通用函數(shù)的功能。

1、before_first_request:在處理第一個(gè)請(qǐng)求前執(zhí)行

before_first_request

在對(duì)應(yīng)用程序?qū)嵗牡谝粋€(gè)請(qǐng)求之前注冊(cè)要運(yùn)行的函數(shù), 只會(huì)執(zhí)行一次

  #: 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) 

將要運(yùn)行的函數(shù)存放到before_first_request_funcs 屬性中進(jìn)行保存

2、before_request:在每次請(qǐng)求前執(zhí)行

在每個(gè)請(qǐng)求之前注冊(cè)一個(gè)要運(yùn)行的函數(shù), 每一次請(qǐng)求都會(huì)執(zhí)行

  #: 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

將要運(yùn)行的函數(shù)存放在字典中, None 為鍵的列表中存放的是整個(gè)應(yīng)用的所有請(qǐng)求都要運(yùn)行的函數(shù).

3、after_request:每次請(qǐng)求之后調(diào)用,前提是沒有未處理的異常拋出

在每個(gè)請(qǐng)求之后注冊(cè)一個(gè)要運(yùn)行的函數(shù), 每次請(qǐng)求都會(huì)執(zhí)行. 需要接收一個(gè) Response 類的對(duì)象作為參數(shù) 并返回一個(gè)新的Response 對(duì)象 或者 直接返回接受到的Response 對(duì)象

  #: 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:每次請(qǐng)求之后調(diào)用,即使有未處理的異常拋出

注冊(cè)一個(gè)函數(shù)在每個(gè)請(qǐng)求的末尾運(yùn)行,不管是否有異常, 每次請(qǐng)求的最后都會(huì)執(zhí)行.

  #: 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

將要運(yùn)行的函數(shù)存放在字典中, None 為鍵的列表中存放的是整個(gè)應(yīng)用的所有請(qǐng)求都要運(yùn)行的函數(shù).

from flask import Flask
app = Flask(__name__)

@app.before_first_request
def before_first_request():
  print('before_first_request')


@app.before_request
def before_request():
  print('before_request')


@app.after_request
def after_request(resp):
  print('after_request')
  return resp


@app.teardown_request
def teardown_request(e):
  print('teardown_request')


@app.route("/")
def view_fn():
  return "view_fn"
  
if __name__ == "__main__":
  app.run()

第一次請(qǐng)求:

頁(yè)面輸出:view_fn
控制臺(tái)輸出: before_first_request
            before_request
            after_request
            teardown_request

第二次請(qǐng)求:

頁(yè)面輸出:view_fn
控制臺(tái)輸出: before_request
            after_request
            teardown_request

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中json模塊load/loads方法實(shí)戰(zhàn)以及參數(shù)詳解

    Python中json模塊load/loads方法實(shí)戰(zhàn)以及參數(shù)詳解

    經(jīng)常在Python中對(duì)JSON格式的文件進(jìn)行操作,今天對(duì)這些操作做一個(gè)總結(jié),下面這篇文章主要給大家介紹了關(guān)于Python中json模塊load/loads方法實(shí)戰(zhàn)以及參數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • twilio python自動(dòng)撥打電話,播放自定義mp3音頻的方法

    twilio python自動(dòng)撥打電話,播放自定義mp3音頻的方法

    今天小編就為大家分享一篇twilio python自動(dòng)撥打電話,播放自定義mp3音頻的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2019-08-08
  • 利用Python繪制一個(gè)可愛的米老鼠

    利用Python繪制一個(gè)可愛的米老鼠

    turtle庫(kù)是一個(gè)點(diǎn)線面的簡(jiǎn)單圖像庫(kù),在Python2.6之后被引入進(jìn)來(lái),能夠完成一些比較簡(jiǎn)單的幾何圖像可視化。本文將利用turtle繪制一個(gè)可愛的米老鼠,感興趣的可以試一試
    2022-03-03
  • 詳解Python中常用的圖片處理函數(shù)的使用

    詳解Python中常用的圖片處理函數(shù)的使用

    這篇文章主要為大家介紹了一些在Python中常用的圖片處理函數(shù)的使用,例如split()、merge()、threshold()、applyColorMap()等,需要的可以參考一下
    2022-01-01
  • python處理文本文件并生成指定格式的文件

    python處理文本文件并生成指定格式的文件

    本節(jié)主要介紹了python如何處理文本文件并生成指定格式的文件,需要的朋友可以參考下
    2014-07-07
  • python?包之?APScheduler?定時(shí)任務(wù)

    python?包之?APScheduler?定時(shí)任務(wù)

    這篇文章主要介紹了python?包之?APScheduler?定時(shí)任務(wù),文章基于python的相關(guān)資料展開主題內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-04-04
  • django的ORM模型的實(shí)現(xiàn)原理

    django的ORM模型的實(shí)現(xiàn)原理

    這篇文章主要介紹了django的ORM模型的實(shí)現(xiàn)原理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2019-03-03
  • python爬蟲beautiful?soup的使用方式

    python爬蟲beautiful?soup的使用方式

    這篇文章主要介紹了python爬蟲beautiful?soup的使用方式,Beautiful?Soup依據(jù)給定的解釋器來(lái)解析html文檔,其依據(jù)html中標(biāo)簽把html文檔在內(nèi)存中轉(zhuǎn)化為類似于二叉樹的數(shù)據(jù)結(jié)構(gòu),并通過實(shí)現(xiàn)的查詢方法來(lái)查詢二叉樹以得到我們想要的爬蟲數(shù)據(jù)
    2022-08-08
  • Python的爬蟲程序編寫框架Scrapy入門學(xué)習(xí)教程

    Python的爬蟲程序編寫框架Scrapy入門學(xué)習(xí)教程

    Python的一大優(yōu)勢(shì)就是可以輕松制作Web爬蟲,而超高人氣的Scrapy則是名副其實(shí)的Python編寫爬蟲的利器,這里我們就來(lái)看一下Python的爬蟲程序編寫框架Scrapy入門學(xué)習(xí)教程:
    2016-07-07
  • 深度學(xué)習(xí)詳解之初試機(jī)器學(xué)習(xí)

    深度學(xué)習(xí)詳解之初試機(jī)器學(xué)習(xí)

    機(jī)器學(xué)習(xí)可應(yīng)用在各個(gè)方面,本篇將在系統(tǒng)性進(jìn)入機(jī)器學(xué)習(xí)方向前,初步認(rèn)識(shí)機(jī)器學(xué)習(xí),利用線性回歸預(yù)測(cè)波士頓房?jī)r(jià),讓我們一起來(lái)看看吧
    2021-04-04

最新評(píng)論

承德市| 神池县| 高雄市| 南华县| 加查县| 彰化县| 娄烦县| 顺昌县| 紫金县| 和平县| 莒南县| 郴州市| 新邵县| 富平县| 吴忠市| 筠连县| 团风县| 贵定县| 类乌齐县| 甘孜| 长顺县| 商城县| 绥芬河市| 岳西县| 上杭县| 玉龙| 北海市| 肥城市| 固阳县| 马尔康县| 安仁县| 海伦市| 镇康县| 台山市| 彭泽县| 临夏县| 始兴县| 秀山| 永定县| 汉源县| 蚌埠市|