例如這裏咱們模擬,用戶訪問首頁的時候,須要先登陸,跳轉到登陸頁面後,模擬用戶輸入的請求是錯誤,咱們返回給他401的狀態(禁止訪問)html
# coding:utf-8 from flask import Flask, redirect, url_for, abort # 這裏咱們須要導入abort這個類 app = Flask(__name__) @app.route('/') def index(): # 將視圖重定向到login頁面 return redirect(url_for('login')) def this_is_never_excuted(): # 永遠不會被執行 pass @app.route("/login", methods=['GET']) def login(): # 模擬登錄失敗, 返回401意爲着禁止訪問 abort(401) print('永遠不會去執行') this_is_never_excuted() if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)
(Flask_py) python@python-VirtualBox:~/code$ python abort_demo.py * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 294-731-034 192.168.3.5 - - [23/Jul/2019 07:41:58] "GET / HTTP/1.1" 302 - 192.168.3.5 - - [23/Jul/2019 07:41:58] "GET /login HTTP/1.1" 401 - 192.168.3.5 - - [23/Jul/2019 07:41:58] "GET /favicon.ico HTTP/1.1" 404 -
經過源碼咱們能夠看到abort函數給出的註釋裏還有一種應用方法前端
''' Can be passed a WSGI application or a status code. If a status code is given it's looked up in the list of exceptions and will raise that exception, if passed a WSGI application it will wrap it in a proxy WSGI exception and raise that:: abort(404) abort(Response('Hello World')) '''
大概翻譯的就是:python
你能夠傳遞一個WSGI應用或者是一個狀態碼。若是給定一個狀態碼,它會在異常列表中查找並引起異常,若是傳遞的是一個WSGI應用,它將把它包裝在一個代理WSGI異常中並引起該異常json
# abort 的另外一種應用方法 from flask import Flask, redirect, url_for, abort, Response # 須要導入的包 app = Flask(__name__) @app.route('/') def index(): # 將視圖重定向到login頁面 return redirect(url_for('login')) def this_is_never_excuted(): # 永遠不會被執行 pass @app.route("/login", methods=['GET']) def login(): # 模擬登錄失敗, 返回401意爲着禁止訪問 # abort(401) abort(Response('禁止訪問!!!')) print('永遠不會去執行') this_is_never_excuted() if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)
# coding:utf-8 from flask import Flask, redirect, url_for, abort, Response app = Flask(__name__) @app.route('/') def index(): # 將視圖重定向到login頁面 return redirect(url_for('login')) def this_is_never_excuted(): # 永遠不會被執行 pass @app.route("/login", methods=['GET']) def login(): # 模擬登錄失敗, 返回401意爲着禁止訪問 abort(404) # abort(Response('禁止訪問!!!')) # print('永遠不會去執行') # this_is_never_excuted() # 自定義錯誤視圖,用來返回給前端用戶看到的結果 @app.errorhandler(404) def page_not_found(error): return "頁面未找到 %s" % error, 404 if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)
# coding:utf-8 from flask import Flask, make_response app = Flask(__name__) @app.route("/index") def index(): # 1 使用元祖,返回自定義的響應信息 # 響應體 狀態碼 響應頭 # return "index page", 400, [("Programme_L", "python"), ("Locality", "Harbin")] # return "index page", 400, {"Programme_L": "python", "Locality": "Harbin"} # return "index page", "666 userDefined status", {"Programme_L": "python", "Locality": "Harbin"} # return "index page", "666 userDefined status" # 2 使用make_response 響應信息 resp = make_response("index page 2") resp.status = "999 userDefined" # 設置狀態碼 resp.headers["city"] = "Harbin" # 設置響應頭 return resp if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)
# coding:utf-8 import json from flask import Flask app = Flask(__name__) @app.route("/index") def index(): data = { 'name': 'circle', 'age': 30, } json_str = json.dumps(data) return json_str, 200, {"Content-Type": "application/json"} # 這裏咱們必定要手動添加這句話來指定爲json數據 if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)
# coding:utf-8 from flask import Flask, jsonify app = Flask(__name__) @app.route("/index") def index(): data = { 'name': 'circle', 'age': 30, } # json_str = json.dumps(data) # return json_str, 200, {"Content-Type": "application/json"} return jsonify(data) if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)