- 論壇徽章:
- 0
|
最新文檔地址:http://pynotes.appspot.com/static/bottle/index.htm
![]()
譯者: smallfish <smallfish.xy@gmail.com>
原文: http://bottle.paws.de/
文檔: 簡(jiǎn)體中文
Bottle是一個(gè)使用Python語言編寫的符合WSGI規(guī)范Web框架.它提供根據(jù)URL參數(shù)轉(zhuǎn)發(fā)請(qǐng)求(映射),模板,key/value數(shù)據(jù)庫,內(nèi)置多種第三方WSGI/HTTP服務(wù)器適配器和模板引擎.整個(gè)框架只有一個(gè)文件不依賴第三方擴(kuò)展庫.
安裝或依賴你可以使用easy_install bottle或下載bottle.py到你工作目錄下.Python版本要求2.5+或3.x(使用2to3轉(zhuǎn)換)
特性和例子輕量級(jí)不需要安裝和配置.你只要獲得一份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)行它并打開瀏覽器訪問http://localhost:8080/.
良好的URL風(fēng)格根據(jù)映射語法提取動(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靜態(tài)文件,重定向和HTTP錯(cuò)誤下面的方法可以方便的處理日常工作.
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可以當(dāng)做一個(gè)簡(jiǎn)單的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自帶了一個(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 templateHTTP服務(wù)器Bottle自帶了HTTP服務(wù)器,當(dāng)然也支持在cherrypy, flup,paste和fapws3運(yùn)行應(yīng)用.
from bottle import PasteServer
run(server=PasteServer)缺少的特性和Bug缺陷Bottle不包括:
- ORM:你可以選擇(SQLAlchemy, Elixir)
- HTML幫助類,會(huì)話,認(rèn)證:自己動(dòng)手吧
- 擴(kuò)展:抱歉還沒有
有些可能不會(huì)工作:
- 在Python 3.x下多文件上傳會(huì)有問題,因?yàn)閏gi.FileStorage修改了.
Benchmark本地使用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)授權(quán)(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. |
|