使用bottle进行web开发(4):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开发(4):HTTPError的更多相关文章
- 使用bottle进行web开发(5):Generating Content
在纯粹的 WSGI中,你的应用能返回的数据类型是十分有限的,你必须返回可迭代的字符串,你能返回字符串是因为字符串是可以迭代的,但是这导致服务器将你的内容按一字符一字符的传送,这个时候,Unicode ...
- 使用bottle进行web开发(1):hello world
为什么使用bottle?因为简单,就一个py文件,和其他模块没有依赖,3000多行代码. http://www.bottlepy.org/docs/dev/ 既然开始学习,就安装它吧. pip3 in ...
- 使用bottle进行web开发(9):文件上传;json传递
1.文件上传 如果要完成文件上传,则需要对上文的form做一点改动,具体如下: <form action="/upload" method="post" ...
- 使用bottle进行web开发(8):get的参数传递,form里的额数据传递等
1.诸如:forum?id=1&page=5这样的,在bottle里,可以通过request.query来访问这些值,举例如下: from bottle import Bottle,run,r ...
- 使用bottle进行web开发(2):http request
我们知道,http request有多个方法,比如get,post,delete,patch,put等.对用的,bottle都定义了相应的装饰器,目前定义了五个: get(),post(),put() ...
- 使用bottle进行web开发(6):Response 对象
Response的元数据(比如http的status code,headers,cookies等,都被i封装到一个叫Response的对象中,并传给浏览器. status code:status co ...
- 使用bottle进行web开发(3):静态文件的获取
静态文件(比如css啊,需要下载的各位文件等),需要通过static_file来操作,首先记得要在import中导入 @route('/static/<filename>') def se ...
- python bottle框架(WEB开发、运维开发)教程
教程目录 一:python基础(略,基础还是自己看书学吧) 二:bottle基础 python bottle web框架简介 python bottle 框架环境安装 python bottle 框架 ...
- bottle+cherrypy快速开发web服务
我目前用得最顺手的python web框架是bottle,简单方便. bottle有一个开发用的http服务器,效率不高,单线程,阻塞. 所以,得找个别的服务器来部署. 根据bottle官方的文档,发 ...
随机推荐
- MySQL中Alter用法小结
alter 方法是我们在处理MySQL数据库中一个常见的方法,能帮助我们更好的处理数据库中的表 1.增加 数据库中表的字段:alter table table_name add [column] co ...
- Nova Cell
Nova Cell V2 详解 现在 ,OpenStack 在控制平面上的性能瓶颈主要在 Message Queue 和 Database . 尤其是 Message Queue , 随着计算节点的增 ...
- Extjs的API阅读方式(整理)
原文链接:http://www.cnblogs.com/gaojun/archive/2013/05/28/3103908.html
- vue2.0 vue-cli项目中路由之间的参数传递
1.首先配置路由, import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) export default new R ...
- Byte数据类型—Java
字节与字符 一个英文字母(不分大小写)占一个字节的空间,一个中文汉字占两个字节,一个二进制数字序列,在计算机中作为一个数字单元,一般为8位二进制数,换算为十进制最小值为0,最大值为255. UTF-8 ...
- Hibernate关联映射之_多对一
多对一 Employee-Department 对于 员工 和 部门 两个对象,从员工的角度来看,就是多对一的一个关系--->多个员工对应一个部门 表设计: 部门表:department,id主 ...
- SRM710 div1 MagicNim(博弈论)
题目大意: 给出n+1堆石子,前n堆石子的数量是a[i],最后一堆只有1个石子,但是具有魔力 拿走该石子的一方可以选择接下来是进行普通的Nim游戏还是anti-nim游戏 问是先手必胜还是必败 首先拿 ...
- [Leetcode] Roman to integer 罗马数字转成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- clique 解题报告
clique 题目描述 数轴上有 \(n\) 个点,第 \(i\) 个点的坐标为 \(x_i\),权值为 \(w_i\).两个点 \(i\),\(j\) 之间存在一条边当且仅当 \(abs(x_i-x ...
- 关于final局部变量引用的研究
嵌套类(内部类)方法安全引用外部方法局部变量的原理 嵌套类方法引用外部局部变量,必需将声明为final,否则将出现 Cannot refer to a non-final variable * ins ...