用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 的基础理论知识不在这里展开,已经有足够的参考资料, ...
随机推荐
- UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)
在本人之前的一篇文章<<Appium基于安卓的各种FindElement的控件定位方法实践和建议>>第二章节谈到Appium可以通过使用UIAutomator的方法去定位And ...
- EXCEL导入GridView,然后再汇入数据库.
原文:EXCEL导入GridView,然后再汇入数据库. 近日项目中有一个多笔料号要输入,我做了一个用javascript复制输入框的功能,可以输入多笔料号. 但是使用者反馈,料号太多,可能几百个料号 ...
- HEAP CORRUPTION DETECTED
发生主要是由于这个问题给写入超出预分配的空间,注意检查越界情况 版权声明:本文博客原创文章,博客,未经同意,不得转载.
- 编程算法 - 二叉树的深度 代码(C)
二叉树的深度 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一棵二叉树的根节点, 求该树的深度. 依次选择最深的左右子树, 然后递归加1. ...
- 使用八种牛云存储解决方案ios7.1的app部署问题
使用八种牛云存储解决方案ios7.1的app部署问题 一个.问题叙述性说明 开发完ios版本号的app.须要将.ipa文件和.plist文件打包上传,供用户下载,在线安装.用户安装过程简单描写叙述例如 ...
- CruiseControl.Net全面实现持续集成
使用CruiseControl.Net全面实现持续集成 持续集成想必大家很多人都听说过,甚至都实践过,最近我又一次亲历了一次持续集成,现将我的经验分享给大家.关于持续集成的理论在本文概不涉及,本文 ...
- 【jar包】图片的异步加载--【 Imageloader】
Android Imageloader图片异步加载 Imageloader是一个在android平台下简单的下载.显示.缓存空间的图片加载库. 异步下载网络图片并可以在UI线程更新View,使用二级缓 ...
- jquery插件推荐
jQuery 是继 prototype 之后又一个优秀的 Javascript 框架.其宗旨是—写更少的代码,做更多的事情.它是轻量级的 js 库(压缩后只有21k) ,这是其它的 js 库所不及的, ...
- NServiceBus 入门2
NServiceBus官方文档翻译(二)NServiceBus 入门 在这篇教程中我们将学习如何创建一个非常简单的由客户端向服务端发送消息的订单系统.该系统包括三个项目:Client.Server ...
- java class load
https://mp.weixin.qq.com/s?__biz=MjM5NzMyMjAwMA==&mid=403638649&idx=2&sn=4f17e8b58c64875 ...