Flask 的鉤子函數(shù)使用
Flask 框架提供了鉤子函數(shù)的機(jī)制用于在特定的位置執(zhí)行用戶注冊的函數(shù),本小節(jié)講解了常見的鉤子函數(shù)的使用。
1. 鉤子簡介
1.1 什么是鉤子
在服務(wù)端處理請求時,有時需要:在處理請求前執(zhí)行一些準(zhǔn)備工作,在處理請求后執(zhí)行一些掃尾工作,例如:
@app.route('/page1')
def page1():
執(zhí)行準(zhǔn)備工作
執(zhí)行請求
執(zhí)行掃尾工作
@app.route('/page2')
def page2():
執(zhí)行準(zhǔn)備工作
執(zhí)行請求
執(zhí)行掃尾工作
程序包括有 2 個頁面:page1 和 page2,在每個頁面處理函數(shù)的頭部,執(zhí)行準(zhǔn)備工作;在每個頁面處理函數(shù)的尾部,執(zhí)行掃尾工作。
以上程序存在的明顯的代碼重復(fù)問題,為了讓每個頁面處理函數(shù)避免編寫重復(fù)功能的代碼,F(xiàn)lask 提供了鉤子函數(shù)機(jī)制:Flask 給應(yīng)用程序提供掛載點(diǎn),在掛載點(diǎn)調(diào)用應(yīng)用程序注冊的函數(shù),該函數(shù)被稱為鉤子函數(shù)。
1.2 注冊鉤子
Flask 框架處理請求時,提供了 2 個掛載點(diǎn):before_request 和 after_request,執(zhí)行請求的流程如下:
- 執(zhí)行在掛載點(diǎn) before_request 注冊的鉤子函數(shù);
- 執(zhí)行頁面處理函數(shù);
- 執(zhí)行在掛載點(diǎn) after_request 注冊的鉤子函數(shù)。
Flask 使用裝飾器的語法注冊鉤子函數(shù),如下所示:
@app.before_request
def before_request():
print('before request')
使用裝飾器 @app.before_request 在掛載點(diǎn) before_request 注冊函數(shù) before_request,F(xiàn)lask 在處理每個請求時:
- 首先調(diào)用函數(shù) before_request,打印字符串 ‘before request’;
- 然后再執(zhí)行請求的處理函數(shù)。
1.3 消除代碼重復(fù)
下面使用鉤子函數(shù)機(jī)制消除 1.1 小節(jié)的代碼重復(fù):
@app.before_request
def before_request():
執(zhí)行準(zhǔn)備工作
@app.after_request
def after_request():
執(zhí)行掃尾工作
@app.route('/page1')
def page1():
執(zhí)行請求
@app.route('/page2')
def page2():
執(zhí)行請求
在第 1 行,在掛載點(diǎn) before_request 注冊處理函數(shù),執(zhí)行請求前會調(diào)用該函數(shù)執(zhí)行準(zhǔn)備工作;在第 5 行,在掛載點(diǎn) after_request 注冊處理函數(shù),執(zhí)行請求前后調(diào)用該函數(shù)執(zhí)行掃尾工作。
與 1.1 小節(jié)中的代碼相比,頁面 /page1 和頁面 /page2 的處理函數(shù)得到了簡化,準(zhǔn)備工作和掃尾工作被統(tǒng)一放置在鉤子函數(shù)中進(jìn)行。
2. 常用的鉤子
Flask 框架中常用的鉤子總結(jié)如下:
1. before_first_request
在應(yīng)用程序?qū)嵗牡谝粋€請求之前要運(yùn)行的函數(shù),只會運(yùn)行一次。
2. before_request
在每個請求之前要運(yùn)行的函數(shù),對每一次請求都會執(zhí)行一次。
3. after_request
在每個請求之后要運(yùn)行的函數(shù),對每一次請求都會執(zhí)行一次。
在 after_request 注冊的鉤子函數(shù)的第一個參數(shù)是 response 對象。
4. teardown_request
在每個請求之后要運(yùn)行的函數(shù),對每一次請求都會執(zhí)行一次。
在 teardown_request 注冊的鉤子函數(shù)的第一個參數(shù)是 exception 對象,指向執(zhí)行請求時發(fā)生的錯誤。
5. errorhandler
發(fā)生一些異常時,比如 HTTP 404、HTTP 500 錯誤,或者拋出異常,就會自動調(diào)用該鉤子函數(shù)。
3. 鉤子的基本使用
3.1 程序代碼 basis.py
編寫一個程序 basis.py 分別在處理請求前、處理請求后注冊鉤子函數(shù):
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def index():
print('execute request')
return 'Hello World'
@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(response):
print('after_request')
return response
if __name__ == '__main__':
app.run()
在第 10 行,通過 @app.before_first_request 注冊在第一次處理請求時執(zhí)行的鉤子函數(shù),該函數(shù)打印字符串 ‘before_first_request’。
在第 14 行,通過 @app.before_request 注冊在每次處理請求前執(zhí)行的鉤子函數(shù),該函數(shù)打印字符串 ‘before_request’。
在第 18 行,通過 @app.after_request 注冊在每次處理請求后執(zhí)行的鉤子函數(shù),該函數(shù)打印字符串 ‘a(chǎn)fter_request’。鉤子函數(shù) after_request(response) 的輸入?yún)?shù)是一個 Response 對象,函數(shù)返回的也是一個 Response 對象。
3.2 運(yùn)行程序 basis.py
1. 啟動程序
$ python basis.py
2. 在瀏覽器中首次訪問 http://localhost:5000
3. 觀察控制臺的輸出
before_first_request
before_request
execute request
after_request
應(yīng)用程序在第一個請求之前要運(yùn)行一次 before_first_request,只會運(yùn)行一次。
4. 在瀏覽器中再次訪問 http://localhost:5000
5. 觀察控制臺的輸出
before_request
execute request
after_request
在程序整個生命周期內(nèi),before_first_request,只會運(yùn)行一次,因此再次訪問網(wǎng)站時,不會再執(zhí)行 before_first_request。
4. after_request 與 teardown_request 的區(qū)別
4.1 程序代碼 diff.py
本節(jié)通過一個具體的例子講解 after_request 與 teardown_request 的區(qū)別,代碼如下:
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def index():
print('execute request without error')
return 'Hello World'
@app.route('/error')
def error():
print('execute request with error')
1 / 0
return 'Hello World'
@app.after_request
def after_request(response):
print('after_request')
return response
@app.teardown_request
def teardown_request(exception):
print('teardown_request:', exception)
if __name__ == '__main__':
app.run()
在第 5 行,訪問頁面 / 時執(zhí)行函數(shù) index(),該函數(shù)打印字符串 ‘execute request without error’,執(zhí)行期間沒有發(fā)生異常。
在第 10 行,訪問頁面 /error 時執(zhí)行函數(shù) error(),該函數(shù)打印字符串 ‘execute request with error’,執(zhí)行期間發(fā)生異常。在第 13 行,人為的通過 1 / 0 引發(fā)除零異常。
在第 16 行,注冊 after_request 鉤子函數(shù),執(zhí)行請求后會調(diào)用該鉤子函數(shù)。
在第 21 行,注冊 teardown_request 鉤子函數(shù),執(zhí)行請求后都會調(diào)用該鉤子函數(shù)。鉤子函數(shù)的第一個參數(shù)是 exception 對象,指向執(zhí)行請求時發(fā)生的錯誤。
4.2 運(yùn)行程序 diff.py
1. 啟動程序 diff.py
$ python diff.py
2. 在瀏覽器中訪問 http://localhost:5000
3. 觀察控制臺的輸出
execute request without error
after_request
teardown_request: None
訪問頁面 / 時,執(zhí)行處理函數(shù) index,執(zhí)行請求結(jié)束后,會調(diào)用 after_request 和 teardown_request。該函數(shù)執(zhí)行期間沒有發(fā)生異常,傳遞給 teardown_request 鉤子函數(shù)的 exception 對象為 None。
4. 在瀏覽器中訪問 http://localhost:5000/error
5. 觀察控制臺的輸出
execute request with error
Traceback (most recent call last):
File "diff.py", line 14, in error
1 / 0
ZeroDivisionError: division by zero
after_request
teardown_request: division by zero
訪問頁面 /error 時,執(zhí)行處理函數(shù) error,執(zhí)行請求結(jié)束后,會調(diào)用 after_request 和 teardown_request。該函數(shù)執(zhí)行期間發(fā)生異常,傳遞給 teardown_request 鉤子函數(shù)的 exception 對象為 ZeroDivisionError。
5. 錯誤處理
5.1 程序代碼 error.py
通過一個具體的例子講解使用 errorhandler 處理 HTTP 錯誤,代碼如下:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
print('execute request')
return 'Hello World'
@app.errorhandler(404)
def errorhandler(e):
print('error_handler(404)')
print(e)
return '404 Error'
if __name__ == '__main__':
app.run()
在第 10 行,使用 @errorhandler(404) 注冊一個鉤子函數(shù) errorhandler(e),當(dāng)發(fā)生 HTTP 404 錯誤(頁面不存在)時,調(diào)用鉤子函數(shù),該函數(shù)的第一個參數(shù)是錯誤對象。
5.2 運(yùn)行程序 error.py
1. 啟動程序 error.py
$ python error.py
2. 在瀏覽器中訪問 http://localhost:5000/non-exist-page
3. 觀察瀏覽器的顯示
404 Error
訪問一個不存在的頁面 /non-exist-page,產(chǎn)生 HTTP 404 錯誤,F(xiàn)lask 框架執(zhí)行使用 @errorhandler(404) 注冊的函數(shù) errorhandler,該函數(shù)返回字符串 ‘404 Error’ 給瀏覽器作為響應(yīng)。
5. 觀察控制臺的輸出
error_handler(404)
404 Not Found: The requested URL was not found on the server. If
URL manually please check your spelling and try again.
執(zhí)行 errorhandler 時,首先打印字符串 ‘error_handler(404)’,然后打印 Error 對象。
6. 源代碼下載
7. 小結(jié)
本小節(jié)講解了鉤子函數(shù)的語法和常見的鉤子函數(shù),對本小節(jié)講解的重點(diǎn),使用思維導(dǎo)圖概括如下: