[譯]Bottle中文簡介

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

Botle Logo

譯者: smallfish <[email protected]>

原文: 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 ,pastefapws3 運行應用.

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.

發佈了44 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章