返回码

在开发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. 使用Net.Mail、CDO组件、JMail组件三种方式发送邮件

    原文:使用Net.Mail.CDO组件.JMail组件三种方式发送邮件 一.使用Net.Mail 需要服务器认证,大部分服务器端口为25. { MailMessage mailMsg = mailMs ...

  2. 使用 COM 风格的编程接口

    使用COM 风格的编程接口 假设不直接使用 COM 库.不创建自己的包装.那么更可能的是使用 COM 风格的编程接口.这是由于如今很多开发商公布应用程序时.提供了首选的互操作程序集(Primary I ...

  3. Android EventBus现实 听说你out该

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/40794879.本文出自:[张鸿洋的博客] 1.概述 近期大家面试说常常被问到Ev ...

  4. Visual Studio 2015环境

    Visual Studio 2015环境搭建 2014年11月13日,微软发布了Visual Studio 2015 Preview,跟随者Visual Studio 2015 而来的是,.net 开 ...

  5. CSS学习笔记:Media Queries

    CSS3提供了Media Queries(媒体查询)的概念,可以利用它查询以下数据: 1.浏览器窗口的宽和高: 2.设备的宽和高: 3.设备的手持方向,横向/竖向: 4.分辨率. @media规则的语 ...

  6. CentOS 7 结构体GCC 4.8.2 32位编译环境

    centos 7 结构体gcc 32位编译环境 1介绍 1.1背景 学习新 C++ 2011和C11标准. 1.2使用软件 CentOS 7(Linux version 3.10.0-123.el7. ...

  7. leetcode第32题--Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  8. leetcode第23题--Swap Nodes in Pairs

    Problem: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1 ...

  9. 让.NET程序快速释放内存的办法

    原文:让.NET程序快速释放内存的办法 公司里的一个程序,经过了N个人的手后发现上了生产内存会一直涨,直到物理内存几乎被占用完毕后突然就下降下来(估计是GC给释放了),然后再一直涨.这个程序主要是对字 ...

  10. Javascript实例技巧精选(7)—设置和获取文本框与文本域的光标位置(兼容IE和Chrome,Firefox)

    >>点击这里下载完整html源码<< 截图如下: 本实例描述了如何用Javascript来控制和获取文本框/文本域的鼠标光标位置,以下代码兼容IE和Chrome,Firefox ...