五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > [譯]Bottle中文簡(jiǎn)介

[譯]Bottle中文簡(jiǎn)介

文章來(lái)源:365jz.com     點(diǎn)擊數(shù):335    更新時(shí)間:2009-09-24 22:31   參與評(píng)論

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

Botle Logo

譯者: 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)換)

特性和例子

輕量級(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)行它并打開瀏覽器訪問(wèn)http://localhost:8080/ .

良好的URL風(fēng)格

根據(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
靜態(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() 來(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
HTTP服務(wù)器

Bottle自帶了HTTP服務(wù)器,當(dāng)然也支持在cherrypy , flup ,paste 和fapws3 運(yùn)行應(yīng)用.

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

Bottle不包括:

  • ORM:你可以選擇(SQLAlchemy, Elixir)
  • HTML幫助類,會(huì)話,認(rèn)證:自己動(dòng)手吧
  • 擴(kuò)展:抱歉還沒(méi)有

有些可能不會(huì)工作:

  • 在Python 3.x下多文件上傳會(huì)有問(wèn)題,因?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 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)入論壇

發(fā)表評(píng)論 (335人查看,0條評(píng)論)
請(qǐng)自覺(jué)遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
昵稱:
最新評(píng)論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時(shí)間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號(hào)