使用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官方的文档,发 ...
随机推荐
- C - 安装雷达
C - 安装雷达 Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem Descr ...
- 通过NTP(Network Time Protocal)协议进行时间同步
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwUAAAKOCAYAAAD3ZbXWAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjw
- servletContext的定义
- 大步小步算法模板题, poj2417
大步小步模板 (hash稍微有一点麻烦, poj不支持C++11略坑) #include <iostream> #include <vector> #include <c ...
- SetLocalTime设置本地时间
/***************************************************************** 函数名:EnableSetTimePriviledge 功 能:开 ...
- 【COGS 2434】 暗之链锁 树上差分+LCA
差分就是把一个值拆成许多差的和如 1 2 4 6 9 那么 把这个东西拆成 1 1 2 2 3 就是了,当然也可以理解为对一个问题分解为多个子问题并对其进行操作来得到原问题的答案. 树上差分就更玄妙了 ...
- 实际上ECMAScript中并没有对类的定义
首先,我们用一个经典例子来简单阐述一下ECMAScript中的继承机制. 在几何学上,实质上几何形状只有两种,即椭圆形(是圆形的)和多边形(具有一定数量的边).圆是椭圆的一种,它只有一个焦点.三角形. ...
- Intellij Idea debug 远程部署的的tomcat项目
web项目部署到tomcat上之后,有时需要打断点单步调试,如果用的是Intellij idea,可以通过如下方法实现: 开启debug端口,启动tomcat 以tomcat7.0.75为例,打开bi ...
- async的用法
package com.example.administrator.myapplication; import android.os.AsyncTask; import android.util.Lo ...
- python爬取七星彩的开奖历史记录
1.因为人不可能一直无休止的学习,偶尔也想做点儿别的,昨天无聊就想写写Python,当然我承认我上班后基本都是在学工作方面的事情,在这个岗位我也呆了三年多了,还是那句话问我什么会不会我会给你说我啥都会 ...