用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 的基础理论知识不在这里展开,已经有足够的参考资料, ...
随机推荐
- javascript7
语句:条件,循环,跳转, 表达式语句,复合语句和空语句,声明语句,var,function,条件语句,switch,循环,标签语句,break语句,continue语句,return语句,throw语 ...
- Appium Android Bootstrap源码分析之简介
在上一个系列中我们分析了UiAutomator的核心源码,对UiAutomator是怎么运行的原理有了根本的了解.今天我们会开始另外一个在安卓平台上基于UiAutomator的新起之秀--Appium ...
- 关于SQL Server 2005 的自动远程数据库备份
原文:(原创)关于SQL Server 2005 的自动远程数据库备份 由于项目需要,需要对目标服务器上的数据库每天进行备份并转移,查阅网上的一些帮助,结合自己的实际需要,写了这篇文章,希望对有同样需 ...
- PHP 以POST方式提交XML、获取XML,最后解析XML
以POST方式提交XML // Do a POST $data="<?xml version='1.0' encoding='UTF-8'?> <TypeRsp> & ...
- PHP 调用asp.net Web Services服务问题总结
原文:PHP 调用asp.net Web Services服务问题总结 PHP是弱类型语言,转换非常不方便. < ?php //soap 客户端 $client=new SoapClient(' ...
- JQuery在Ajax的Post提交中国乱码的解决方案
介绍: 在JQuery的Ajax POST要求,一个要求.中国的背景之中,乱码,如何解决呢? 问题简介: var regid = $('#oregion').combobox('getValue'); ...
- Java集合之Stack 源码分析
1.简介 栈是数据结构中一种很重要的数据结构类型,因为栈的后进先出功能是实际的开发中有很多的应用场景.Java API中提供了栈(Stacck)的实现,简单使用如下所示 package com.tes ...
- 谈Linux
新手谈Linux 目录: 什么是Linux? Linux与UNIX的区别 Linux与Windows比较 什么是Linux发布版? Linux应用领域 Linux版本的选择 怎么学习Linux? ...
- 数据类型 text 和 varchar 在 add 运算符中不兼容
原文:数据类型 text 和 varchar 在 add 运算符中不兼容 在SQL Server2005中,使用类似下面的Update语句: 1 UPDATE tb_SmsBlacklist SET ...
- SQL Server中如何备份存储过程(SP)和函数(Fun)
考虑到安全因素,我们经常需要对数据库的存储过程(SP)和函数(Fun)进行备份 下面提供了一种简单的方式, 存储过程(SP)SQL代码如下: select p.name as SpName,m.def ...