现在的位置: 首页 > 综合 > 正文

[译]Bottle中文简介

2014年04月19日 ⁄ 综合 ⁄ 共 3268字 ⁄ 字号 评论关闭

最新文档地址:http://pynotes.appspot.com/static/bottle/index.htm


Botle Logo

译者: smallfish
<smallfish.xy@gmail.com>

原文: http://bottle.paws.de/

文档: 简体中文

Bottle是一个使用Python
语言编写的符合WSGI
规范Web框架.它提供根据URL参数转发请求(映射
),模板
,key/value数据库
,内置多种第三方WSGI/HTTP服务器适配器和模板引擎.整个框架只有一个文件不依赖第三方扩展库.

安装或依赖

你可以使用easy_install bottle
或下载bottle.py
到你工作目录下.Python版本要求2.5+或3.x
(使用2to3转换)

特性和例子

轻量级

不需要安装和配置.你只要获得一份bottle.py拷贝就可以编码了!简单的"Hello World" Bottle应用大致如下:

from bottle import route, run
@route('/')
def index():
return 'Hello World!'
run(host='localhost', port=8080)

就是这么简单.运行它并打开浏览器访问http://localhost:8080/
.

良好的URL风格

根据映射语法提取动态URL参数.

@route('/hello/:name')
def hello(name):
return 'Hello, %s' % name

获取使用完整的正则去匹配.

@route('/friends/(?<name>(Alice|Bob))')
def friends(name):
return 'Hello, %s! Good to see you :)' % name
静态文件,重定向和HTTP错误

下面的方法可以方便的处理日常工作.

from bottle import send_file, redirect, abort
@route('/static/:filename')
def static_file(filename):
send_file(filename, root='/path/to/static/files')
@route('/wrong/url')
def wrong():
redirect("/right/url")
@route('/restricted')
def restricted(): abort(401, "Sorry, access denied.")
POST,GET,Header和Cookies

可以当做一个简单的dict()
来使用

from bottle import request, response
@route('/hello/cookie') def cookie():
name = request.COOKIES.get('name', 'Stranger')
response.header['Content-Type'] = 'text/plain'
return 'Hello, %s' % name
@route('/hello/cookie', method='POST')
def set_cookie():
if 'name' in request.POST:
name = request.POST['name']
response.COOKIES['name'] = name return 'OK'
模板

Bottle自带了一个简单快速的轻量级模板引擎

@get('/hello/template/:names')
def pretty_hello(names): names = names.split(',') return template('hello', title='Hello World', names=names)

这里是模板:

<html>
<head>
<title>{{title}}</title> </head> <body>
%for name in names:
<p>Hello, <strong>{{name}}</strong></p>
%end
</body>
</html>

也可以使用mako
模板获得更多功能

from bottle import mako_template as template
HTTP服务器

Bottle自带了HTTP服务器,当然也支持在cherrypy
, flup
,paste
fapws3
运行应用.

from bottle import PasteServe
run(server=PasteServer)
缺少的特性和Bug缺陷

Bottle不包括:

  • ORM:你可以选择(SQLAlchemy, Elixir)
  • HTML帮助类,会话,认证:自己动手吧
  • 扩展:抱歉还没有

有些可能不会工作:

  • 在Python 3.x下多文件上传会有问题,因为cgi.FileStorage修改了.

Benchmark

本地使用ApacheBench在我的AMD 2800+(2GB)测试Bottle 0.4.2 run(server=PasteServer)

marc@nava:/work/bottle$ ab -c10 -n1000 http://localhost:8080/template/test
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
..
Server Software: PasteWSGIServer/0.5
...
Concurrency Level: 10
Time taken for tests: 2.238 seconds
Complete requests: 1000
...
Requests per second: 446.83 [#/sec] (mean)
Time per request: 22.380 [ms] (mean)
Time per request: 2.238 [ms] (mean, across all concurrent requests)

授权(MIT)

Copyright (c) 2009, Marcel Hellkamp.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

抱歉!评论已关闭.