免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5996 | 回复: 0
打印 上一主题 下一主题

[译]Bottle中文简介 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-24 08:24 |只看该作者 |倒序浏览

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


译者: 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 templateHTTP服务器Bottle自带了HTTP服务器,当然也支持在cherrypy, flup,pastefapws3运行应用.
from bottle import PasteServer
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 personobtaining 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 OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANYCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THESOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP