譯者: smallfish <smallfish.xy@gmail.com>
原文: http://bottle.paws.de/
文檔: 簡(jiǎn)體中文
Bottle是一個(gè)使用Python 語(yǔ)言編寫的符合WSGI 規(guī)范Web框架.它提供根據(jù)URL參數(shù)轉(zhuǎn)發(fā)請(qǐng)求(映射 ),模板 ,key/value數(shù)據(jù)庫(kù) ,內(nèi)置多種第三方WSGI/HTTP服務(wù)器適配器和模板引擎.整個(gè)框架只有一個(gè)文件不依賴第三方擴(kuò)展庫(kù).
你可以使用easy_install bottle
或下載bottle.py
到你工作目錄下.Python版本要求2.5+或3.x
(使用2to3轉(zhuǎn)換)
不需要安裝和配置.你只要獲得一份bottle.py拷貝就可以編碼了!簡(jiǎn)單的"Hello World" Bottle應(yīng)用大致如下:
from bottle import route, run
@route('/')
def index():
return 'Hello World!'
run(host='localhost', port=8080)
就是這么簡(jiǎn)單.運(yùn)行它并打開瀏覽器訪問(wèn)http://localhost:8080/ .
根據(jù)映射語(yǔ)法提取動(dòng)態(tài)URL參數(shù).
@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
下面的方法可以方便的處理日常工作.
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.")
可以當(dāng)做一個(gè)簡(jiǎn)單的dict()
來(lái)使用
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自帶了一個(gè)簡(jiǎn)單快速的輕量級(jí)模板引擎
@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
Bottle自帶了HTTP服務(wù)器,當(dāng)然也支持在cherrypy , flup ,paste 和fapws3 運(yùn)行應(yīng)用.
from bottle import PasteServe run(server=PasteServer)
Bottle不包括:
有些可能不會(huì)工作:
本地使用ApacheBench在我的AMD 2800+(2GB)測(cè)試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)
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.
如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣髡搲瑥V大熱心網(wǎng)友會(huì)為你解答??! 點(diǎn)擊進(jìn)入論壇