使用bottle进行web开发:HTTPError
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用bottle进行web开发:HTTPError相关的知识,希望对你有一定的参考价值。
from bottle import error @error(404) def error404(error): return ‘Nothing here, sorry‘
上述代码,是对404的定义,这里注意,有一个HTTPError,
HTTPError
uses a predefined html template to build the body of the response. Instead of using HTTPError
you can use response
with the appropriate status code and body.
import json from bottle import run, route, response @route(‘/text‘) def get_text(): response.status = 400 return ‘Object already exists with that name‘ @route(‘/json‘) def get_json(): response.status = 400 response.content_type = ‘application/json‘ return json.dumps({‘error‘: ‘Object already exists with that name‘}) # Start bottle server. run(host=‘0.0.0.0‘, port=8070, debug=True)
以上是关于使用bottle进行web开发:HTTPError的主要内容,如果未能解决你的问题,请参考以下文章