用Bottle开发web程序(二)
返回码
在开发web程序时,除了一些服务器错误等,常常需要自定义返回码,以便告诉用户处理请求的结果或者状态。bottle支持自定义的返回码,可以通过以下几种方式进行实现。
abort
在bottle中,如果需要设置返回错误码,可以简单的通过abort函数来设置。返回内容会是一个带有错误信息的页面。
@route('/abort', method='POST')
def do_abort():
data = request.body
data = data.read()
if data != 'abort':
abort(400, 'Your request is not abort.')
return 'abort test.'
试一下:
[root@localhost ~]# curl -g -i 127.0.0.1:9001/abort -X POST -d 'abort'
HTTP/1.0 200 OK
Server: PasteWSGIServer/0.5 Python/2.7.5
Date: Thu, 07 Apr 2016 03:25:52 GMT
Content-Length: 11
Content-Type: text/html; charset=UTF-8
abort test.[root@localhost ~]#
[root@localhost ~]# curl -g -i 127.0.0.1:9001/abort -X POST -d 'test'
HTTP/1.0 400 Bad Request
Server: PasteWSGIServer/0.5 Python/2.7.5
Date: Thu, 07 Apr 2016 03:26:11 GMT
Content-Length: 731
Content-Type: text/html; charset=UTF-8
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>Error: 400 Bad Request</title>
<style type="text/css">
html {background-color: #eee; font-family: sans;}
body {background-color: #fff; border: 1px solid #ddd;
padding: 15px; margin: 15px;}
pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;}
</style>
</head>
<body>
<h1>Error: 400 Bad Request</h1>
<p>Sorry, the requested URL <tt>'http://127.0.0.1:9001/abort'</tt>
caused an error:</p>
<pre>Your request is not abort.</pre>
</body>
</html>
abort函数用起来比较方便,弊端是返回的是页面,比较难于获取具体的出错信息。
abort and error
error函数可以自定义某个error code的返回内容。可以使用abort函数触发该函数。
自定义的error函数的参数是一个HttpError的实例。可以通过error.body获取实例的相关信息。
@route('/code', method='POST')
def do_abort():
data = request.body
data = data.read()
if data != 'code':
abort(499, 'Your request is not code.')
return 'code test.'
@error(499)
def error499(error):
return error.body
试一下:
[root@localhost ~]# curl -g -i 127.0.0.1:9001/code -X POST -d 'test'
HTTP/1.0 499 Unknown
Server: PasteWSGIServer/0.5 Python/2.7.5
Date: Fri, 08 Apr 2016 02:36:04 GMT
Content-Length: 25
Content-Type: text/html; charset=UTF-8
Your request is not code.[root@localhost ~]#
HttpResponse
另外一种方式就是自己设定实例化一个HttpResponse进行返回。
@route('/httpresponse', method='POST')
def do_httpresponse():
data = request.body
data = data.read()
if data != 'httpresponse':
return HTTPResponse(body='Your request is not httpresponse.', status=400)
return 'httpresponse test.'
试一下:
[root@localhost ~]# curl -g -i 127.0.0.1:9001/httpresponse -X POST -d 'test'
HTTP/1.0 400 Bad Request
Server: PasteWSGIServer/0.5 Python/2.7.5
Date: Thu, 07 Apr 2016 03:30:17 GMT
Content-Length: 26
Content-Type: text/html; charset=UTF-8
Your request is not httpresponse.
这种方式比较灵活,而且返回的body同样支持字典的类型。bottle会将其自动转换为json。
@route('/httpresponse', method='POST')
def do_httpresponse():
data = request.body
data = data.read()
if data != 'httpresponse':
return HTTPResponse(body={'info':'Your request is not httpresponse.'}, status=400)
return 'httpresponse test.'
试一下:
[root@localhost ~]# curl -g -i 127.0.0.1:9001/httpresponse -X POST -d 'test'
HTTP/1.0 400 Bad Request
Server: PasteWSGIServer/0.5 Python/2.7.5
Date: Thu, 07 Apr 2016 03:34:55 GMT
Content-Length: 38
Content-Type: application/json
{"info": "Your request is not httpresponse."}
完整样例
import sys
from bottle import abort, run, route , request, HTTPResponse, error
@route('/abort', method='POST')
def do_abort():
data = request.body
data = data.read()
if data != 'abort':
abort(400, 'Your request is not abort.')
return 'abort test.'
@route('/httpresponse', method='POST')
def do_httpresponse():
data = request.body
data = data.read()
if data != 'httpresponse':
return HTTPResponse(body='Your request is not abort.', status=400)
# return HTTPResponse(body={'info':'Your request is not abort.'}, status=400)
return 'httpresponse test.'
@route('/code', method='POST')
def do_abort():
data = request.body
data = data.read()
if data != 'code':
abort(499, 'Your request is not code.')
return 'code test.'
@error(499)
def error499(error):
return error.body
def main():
run(server='paste', host='0.0.0.0', port=9001)
if __name__ == "__main__":
sys.exit(main())
用Bottle开发web程序(二)的更多相关文章
- 用Bottle开发web程序(一)
Bottle Bottle是一个轻量级的web app框架.相较与django等框架,bottle几乎没有任何依赖,而且只有一个文件.而相对于python默认的SimpleHTTPServer,功能更 ...
- ASP.NET 5系列教程 (五):在Visual Studio 2015中使用Grunt、Bower开发Web程序
基于Visual Studio 2015,你可以: 方便的管理前端包,如jQuery, Bootstrap, 或Angular. 自动运行任务,如LESS.JavaScript压缩.JSLint.Ja ...
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
上篇博客使用Spring Boot开发Web项目我们简单介绍了使用如何使用Spring Boot创建一个使用了Thymeleaf模板引擎的Web项目,当然这还远远不够.今天我们再来看看如何给我们的We ...
- Spring boot 整合 Mybatis + Thymeleaf开发web(二)
上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染, ...
- IDEA开发web程序配置Tomcat
1.下载zip版的Tomcat 7,并解压2.在IDEA中配置Tomcat 7 在idea中的Settings(Ctrl+Alt+s)(或者点击图标 ) 弹出窗口左上过滤栏中输入“Applicatio ...
- eclipse 开发web程序,启动tomcat插件服务器的时候。部署目录在那里?
不在tomcat-home/webapps/下面, 你做一个文件上传功能看看就知道了,临时目录一般是你的工作区间workspace\.metadata\.plugins\org.eclipse.wst ...
- 用Delphi7开发Web Service程序 转
转:http://rosehacker.blog.51cto.com/2528968/450160 用Delphi7开发Web Service程序,并把服务程序放在IIS Web服务器上提供给 ...
- 一步一步实现web程序信息管理系统之二----后台框架实现跳转登陆页面
SpringBoot springboot的目的是为了简化spring应用的开发搭建以及开发过程.内部使用了特殊的处理,使得开发人员不需要进行额外繁锁的xml文件配置的编写,其内部包含很多模块的配置只 ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程(二十一):在小程序中使用 WebSocket (.NET Core)
本文将介绍如何在 .NET Core 环境下,借助 SignalR 在小程序内使用 WebSocket.关于 WebSocket 和 SignalR 的基础理论知识不在这里展开,已经有足够的参考资料, ...
随机推荐
- dom02
事件对象:在触发DOM上的事件时dou都会产生一个对象,事件对象event DOM中的事件对象 1)type属性 用于获取事件类型 2)target属性 用于获取事件目标 3)stopPropagat ...
- DELL iDRAC 远程虚拟机报错:虚拟介质分离或所选虚拟磁盘驱动器的虚拟介质重定向已由另一用户使用
原因很简单,那就是虚拟介质的映射功能,只能被使用一次. 推荐做法: 1.由于通过远程,在Lifecycle Controller里DeployOS安装系统,需要在虚拟介质里映射ISO,因此映射功能要留 ...
- GIMP也疯狂之动态图的制作(四)
本篇文章为gimp制作动态图的第四篇.在之前的基础上简单的拓展了下思路.就能做出蛮实用的动态图.本文将介绍两个动态图.第一个为在一张静态图上添加动态图,第二个图为修改部分渐变. 效果: 素材: 其实, ...
- 【SSRS】入门篇(二) -- 建立数据源
原文:[SSRS]入门篇(二) -- 建立数据源 通过 [SSRS]入门篇(一) -- 创建SSRS项目 这篇,我们建立了一个SSRS项目: 接下来,我们以 AdventureWorks2012 示例 ...
- css Cursor:url()自定义鼠标指针样式为图片
css自定义鼠标指针样式为图片Cursor:url()的使用,今天在项目中,要用到自定义鼠标样式,格式: css:{cursor:url('绝对路径的图片(格式:cur,ico)'),-moz-zoo ...
- 用javascript实现2048的小游戏
前段时间,看了一个视频,用javascript实现的2048小游戏,发现不难,都是一些基出的语法和简单逻辑. 整个2048游戏没有很多的数据,所有,实现起来还是很有成就感的. 先上图,简直就和原版游戏 ...
- sql常用语句汇总
--创建数据库 USE yuju CREATE database YuJu on primary ( name='YuJu', filename='B:\ceshi数据库\YuJu.mdf', max ...
- Bootstrap 图标
Bootstrap 图标由 Glyphicons 提供.详情可以去bootstrap官网进行查看. 用法: <i class="icon_class_name">< ...
- 生成自己的Webapi帮助文档(二)
经过今天一上午的修改,已经有个基础的框架了,其它功能只能是在实际使用中发现一个修改一个了. 以下是生成的结果示例: 相比昨天,几个Model都有修改,这里就不一一贴代码了,放个代码包上来,有需要的自己 ...
- Definition of:payload
(1) Refers to the "actual data" in a packet or file minus all headers attached for transpo ...