例如这里咱们模拟,用户访问首页的时候,须要先登陆,跳转到登陆页面后,模拟用户输入的请求是错误,咱们返回给他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)