Flask框架(flask中的请求上下文和应用上下文,以及请求钩子的使用,Flask-Script 扩展命令行)
1.請求上下文與應用上下文
? ?請求上下文(request context) request和session都屬于請求上下文對象。
? ? 應用上下文(application context) current_app和g都屬于應用上下文對象。
? ? ? ?current_app:表示當前運行程序文件的程序實例。
? ? ? ? g對象:處理請求時,用于臨時存儲的對象,每次請求都會重設這個變量。
2.請求鉤子
? ??請求鉤子是通過裝飾器的形式實現,Flask支持如下四種請求鉤子:
(1)?before_first_request:在處理第一個請求前運行。? :? ? 裝飾器:?@app.before_first_request
(2)?before_request:在每次請求前運行。? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 裝飾器:?@app.before_request
(3)?after_request(response):如果沒有未處理的異常拋出,在每次請求后運行。?裝飾器:?@app.after_request
?(4)?teardown_request(response):在每次請求后運行,即使有未處理的異常拋出。
? ? ? ? ? ?裝飾器:?@app.teardown_request
注意,after_request和?teardown_request需要接受參數而且還要有返回值
?如果視圖函數中有異常,則after_request就不會執行,
不管視圖函數有沒有異常都會去執行,前提是app.run(debug=False)
?
from flask import Flask, request, url_forapp = Flask(__name__)@app.route("/index") def index():print("index 被執行")a = 1 / 0return "index page"@app.route("/hello") def hello():print("hello 被執行")return "hello page"@app.before_first_request def handle_before_first_request():"""在第一次請求處理之前先被執行"""print("handle_before_first_request 被執行")@app.before_request def handle_before_request():"""在每次請求之前都被執行"""print("handle_before_request 被執行")@app.after_request def handle_after_request(response):"""在每次請求(視圖函數處理)之后都被執行, 前提是視圖函數沒有出現異常"""print("handle_after_request 被執行")return response@app.teardown_request def handle_teardown_request(response):"""在每次請求 (視圖函數處理)之后都被執行, 無論視圖函數是否出現異常,都被執行, 工作在非調試模式時 debug=False"""print("handle_teardown_request 被執行")return responseif __name__ == '__main__':app.run()?訪問:127.0.0.1:5000/index? ? ,? 我們事先就在視圖函數里面定義了一個錯誤
? 訪問之后,回到程序就可以看到 handle_after_request 沒有被執行
?
?3. 針對于路徑的不同,可以進行不同的處理和邏輯
? ? 定義一個視圖函數,設置路徑,然后在handle_teardown_request里面判斷路徑,針對于不同的路徑,做相應的處理
from flask import Flask, request, url_forapp = Flask(__name__)@app.route("/index") def index():print("index 被執行")a = 1 / 0return "index page"@app.route("/hello") def hello():print("hello 被執行")return "hello page"@app.before_first_request def handle_before_first_request():"""在第一次請求處理之前先被執行"""print("handle_before_first_request 被執行")@app.before_request def handle_before_request():"""在每次請求之前都被執行"""print("handle_before_request 被執行")@app.after_request def handle_after_request(response):"""在每次請求(視圖函數處理)之后都被執行, 前提是視圖函數沒有出現異常"""print("handle_after_request 被執行")return response@app.teardown_request def handle_teardown_request(response):"""在每次請求 (視圖函數處理)之后都被執行, 無論視圖函數是否出現異常,都被執行, 工作在非調試模式時 debug=False"""print("handle_teardown_request 被執行")path = request.path # request.path 請求的路徑# 判斷請求的邏輯if path == url_for("index"):print("在請求鉤子中判斷請求的視圖邏輯: index")elif path == url_for("hello"):print("在請求鉤子中判斷請求的視圖邏輯: hello")print("handle_teardown_request 被執行")return responseif __name__ == '__main__':app.run()? 首先訪問127.0.0.1:5000/index
??
?然后訪問127.0.0.1:5000/hello
4.?Flask-Script 擴展命令行
? ?(1 ) 導入??from flask_script import Manager? ? #? ? Manager 是啟動命令的管理類
? ? ? 創建? ?manager = Manager()? ? ?#?創建Manager管理類的對象
? ? ? ?啟動? ?manager.run()? ???#? 通過管理對象來啟動flask? ?
? (2)? 通過腳本的啟動方式有兩種:
? ? ?①runserver
? ? ? ? ? python 7.4_flask_script.py runserver(py文件名)? ? 命令的方式開啟flask
? ? ? ? ?? python 7.4_flask_script.py runserver -h 0.0.0.0 -p 8000? ? ? ?開啟的時候指定ip和端口號
? ? ? ? ? ?python 7.4_flask_script.py runserver --help 可以查看更多的命令
? ? ? ②? ?shell
? ? ? ??python 7.4_flask_script.py(py文件名)? shell
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Flask框架(flask中的请求上下文和应用上下文,以及请求钩子的使用,Flask-Script 扩展命令行)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Flask框架(flask中设置和获取s
- 下一篇: Flask框架(flask中的邮件发送F