返回码

在开发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程序(二)的更多相关文章

  1. 用Bottle开发web程序(一)

    Bottle Bottle是一个轻量级的web app框架.相较与django等框架,bottle几乎没有任何依赖,而且只有一个文件.而相对于python默认的SimpleHTTPServer,功能更 ...

  2. ASP.NET 5系列教程 (五):在Visual Studio 2015中使用Grunt、Bower开发Web程序

    基于Visual Studio 2015,你可以: 方便的管理前端包,如jQuery, Bootstrap, 或Angular. 自动运行任务,如LESS.JavaScript压缩.JSLint.Ja ...

  3. 使用Spring Boot开发Web项目(二)之添加HTTPS支持

    上篇博客使用Spring Boot开发Web项目我们简单介绍了使用如何使用Spring Boot创建一个使用了Thymeleaf模板引擎的Web项目,当然这还远远不够.今天我们再来看看如何给我们的We ...

  4. Spring boot 整合 Mybatis + Thymeleaf开发web(二)

    上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染, ...

  5. IDEA开发web程序配置Tomcat

    1.下载zip版的Tomcat 7,并解压2.在IDEA中配置Tomcat 7 在idea中的Settings(Ctrl+Alt+s)(或者点击图标 ) 弹出窗口左上过滤栏中输入“Applicatio ...

  6. eclipse 开发web程序,启动tomcat插件服务器的时候。部署目录在那里?

    不在tomcat-home/webapps/下面, 你做一个文件上传功能看看就知道了,临时目录一般是你的工作区间workspace\.metadata\.plugins\org.eclipse.wst ...

  7. 用Delphi7开发Web Service程序 转

        转:http://rosehacker.blog.51cto.com/2528968/450160 用Delphi7开发Web Service程序,并把服务程序放在IIS Web服务器上提供给 ...

  8. 一步一步实现web程序信息管理系统之二----后台框架实现跳转登陆页面

    SpringBoot springboot的目的是为了简化spring应用的开发搭建以及开发过程.内部使用了特殊的处理,使得开发人员不需要进行额外繁锁的xml文件配置的编写,其内部包含很多模块的配置只 ...

  9. Senparc.Weixin.MP SDK 微信公众平台开发教程(二十一):在小程序中使用 WebSocket (.NET Core)

    本文将介绍如何在 .NET Core 环境下,借助 SignalR 在小程序内使用 WebSocket.关于 WebSocket 和 SignalR 的基础理论知识不在这里展开,已经有足够的参考资料, ...

随机推荐

  1. jQuery多文件

    jQuery多文件下载 文件下载是一个Web中非常常用的功能,不过你是做内部管理系统还是做面向公众的互联网公司都会遇到这个问题,对于下载一般有点实际开发经验的都会自己解决,上周弄了一下多文件下载,业务 ...

  2. bootstrap基本标签总结2

    [布局]bootstrap基本标签总结2   缩略图 <div class="container"> <div class="row"> ...

  3. hdu 4858 项目管理(STL集装箱)

    项目管理 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. 题注Oracle数据库的网络连接原理

    版权声明:本文博客原创文章,博客,未经同意,不得转载.

  5. MySQL之数据库结构优化

    1.选择合适的数据类型 一.选择能够存下数据类型最小的数据类型 二.可以使用简单的数据类型.int  要比varchar在MySQL处理上简单 三.尽可能的使用not null  定义字段 四.尽量少 ...

  6. Mockito使用注意事项

    已使用mockito有些问题.例如:配合可变长度参数.定义自己的参数匹配,在这些mockito官方文件(http://docs.mockito.googlecode.com/hg/latest/org ...

  7. Web学习-apache视图log刊物

    视图apache刊物 apache日志位置 不同的系统位置不同. widnows 假如是windows的话,xampp下应该是都存在的,直接去找apache的folder/log/access.log ...

  8. WCF 采用net.tcp协议

    WCF 采用net.tcp协议实践   概述 与Socket相比,WCF真是爽得不得了,其基本指导思想为SOA——面向服务. 其基本配置在于ABC(Address,Binding,Contract), ...

  9. 传说中的WCF(1):这东西难学吗?

    WCF难学吗? 是啊,这问题估计很多人都会问,也包括阿拉在内,也有此深刻而严重的凝问. 也有人说:“如何某项技术可以化繁为简,学起来轻松一点就好了.”也许,人类开生就摆脱不了一种习性——懒惰:不过,也 ...

  10. HTTP压缩算法SDCH

    程序设计中使用的那些共享方法或者技术 前段时间看了个paper是讲述谷歌浏览器使用的压缩方法SDCH,其实原理还是比较简单的. 看了论文后就想总结一下程序中使用的一些共享方法或者技术吧. 1.Goog ...