ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
有些时候,我们往往想在特定的情况下显示一些非200的响应。 强悍的bottle框架又贡献一个强悍的方法给我们秒了这个问题。 它就是abort函数,abort(状态码(int), 输出内容(str)) 下给出例子: 404页面不存在 ~~~ # coding:UTF-8 from bottle import Bottle, abort app = Bottle() @app.get('/') def index(): return abort(404, "页面不存在!") app.run(host="127.0.0.1", port=8000, reloader=True, debug=True) ~~~ 500服务器内部错误 ~~~ # coding:UTF-8 from bottle import Bottle, abort app = Bottle() @app.get('/') def index(): return abort(500, "服务器出错!") app.run(host="127.0.0.1", port=8000, reloader=True, debug=True) ~~~ 403禁止访问 ~~~ # coding:UTF-8 from bottle import Bottle, abort app = Bottle() @app.get('/') def index(): return abort(403, "禁止访问!") app.run(host="127.0.0.1", port=8000, reloader=True, debug=True) ~~~