標籤:content ... gif asa pypi facebook ict 基類 att
bottle 是一個輕量級的python
web架構, 可以適配各種
web伺服器,包括python內建的wsgiref(預設),gevent, cherrypy,gunicorn等等。bottle是單檔案形式發布,源碼在這裡可以下載,代碼量不多,可以用來學習web架構。這裡也有官方文檔的中文翻譯。 首先我們來運行一下bottle的hello world
from bottle import run if __name__ == ‘__main__‘: def application(environ, start_response): start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) return [‘<h1>Hello world!</h1>‘] run(host=‘localhost‘, port=8080, app=application)
上面的代碼看起來也非常符合wsgi的介面規範。啟動改代碼,可以看到輸出 Bottle v0.13-dev server starting up (using
WSGIRefServer())... Listening on http://localhost:8080/ Hit Ctrl-C to quit. 輸出中加粗部分表明使用的web伺服器是python內建的wsgiref。也可以使用其他web server,比如gevent,前提是需要安裝gevent,修改後的代碼如下:
from bottle import runimport gevent.monkeygevent.monkey.patch_all() if __name__ == ‘__main__‘: def application(environ, start_response): start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) return [‘<h1>Hello world!</h1>‘] run(host=‘localhost‘, port=8080, app=application, server = ‘gevent‘)
通過server關鍵字指定web伺服器為‘gevent’,輸出的第一行變成了:
Bottle v0.13-dev server starting up (using
GeventServer())...不管bottle用什麼web伺服器啟動,在瀏覽器輸入127.0.0.1:8080,都可以看到 下面介紹bottle中部分類和介面
bottle.Bottle 代表一個獨立的wsgi應用,由一下部分組成:routes, callbacks, plugins, resources and configuration。 __call__: Bottle定義了__call__函數, 使得Bottle的執行個體能成為一個callable。在前文提到,web架構(或Application)需要提供一個callbale對象給web伺服器,bottle提供的就是Bottle執行個體
def __call__(self, environ, start_response): """ Each instance of :class:‘Bottle‘ is a WSGI application. """ return self.wsgi(environ, start_response)
下面是Bottle.wsgi函數的核心代碼,主要調用兩個比較重要的函數:_handle, _cast
def wsgi(self, environ, start_response): """ The bottle WSGI-interface. """ try: out = self._cast(self._handle(environ)) # rfc2616 section 4.3 if response._status_code in (100, 101, 204, 304) or environ[‘REQUEST_METHOD‘] == ‘HEAD‘: if hasattr(out, ‘close‘): out.close() out = [] start_response(response._status_line, response.headerlist) return out
_handle:處理請求,最終調用到application ,簡化後的代碼如下:
1 def _handle(self, environ):2 self.trigger_hook(‘before_request‘)3 route, args = self.router.match(environ)4 out = route.call(**args)5 self.trigger_hook(‘after_request‘)6 return out
_cast: 標準的wsgi介面對Application的傳回值要求嚴格,必須迭代返回字串。bottle做了一些擴充,可以允許App返回更加豐富的類型,比如dict,File等。 _cast函數對_handle函數傳回值進行處理,使之符合wsgi規範
bottle.Route 封裝了路由規則與對應的回調
bottle.Router
A Router is an ordered collection of route->target pairs. It is used to efficiently match WSGI requests against a number of routes and return the first target that satisfies the request.
ServerAdapter 所有bottle適配的web伺服器的基類,子類只要實現run方法就可以了,bottle裡面有大量的Web伺服器的適配。下表來自官網,介紹了bottle支援的各種web伺服器,以及各自的特性。
| Name |
Homepage |
Description |
| cgi |
|
Run as CGI script |
| flup |
flup |
Run as FastCGI process |
| gae |
gae |
Helper for Google App Engine deployments |
| wsgiref |
wsgiref |
Single-threaded default server |
| cherrypy |
cherrypy |
Multi-threaded and very stable |
| paste |
paste |
Multi-threaded, stable, tried and tested |
| rocket |
rocket |
Multi-threaded |
| waitress |
waitress |
Multi-threaded, poweres Pyramid |
| gunicorn |
gunicorn |
Pre-forked, partly written in C |
| eventlet |
eventlet |
Asynchronous framework with WSGI support. |
| gevent |
gevent |
Asynchronous (greenlets) |
| diesel |
diesel |
Asynchronous (greenlets) |
| fapws3 |
fapws3 |
Asynchronous (network side only), written in C |
| tornado |
tornado |
Asynchronous, powers some parts of Facebook |
| twisted |
twisted |
Asynchronous, well tested but... twisted |
| meinheld |
meinheld |
Asynchronous, partly written in C |
| bjoern |
bjoern |
Asynchronous, very fast and written in C |
| auto |
|
Automatically selects an available server adapter
|
可以看到,bottle適配的web伺服器很豐富。工作模式也很全面,有多線程的(如paste)、有多進程模式的(如gunicorn)、也有基於協程的(如gevent)。具體選擇哪種web伺服器取決於應用的特性,比如是CPU bound還是IO bound
bottle.run 啟動wsgi伺服器。幾個比較重要的參數 app: wsgi application,即可以是bottle.Bottle 也開始是任何滿足wsgi 介面的函數 server: wsgi http server,字串 host:port: 監聽連接埠 核心邏輯: ServerAdapter.run(app)。最後,bottle源碼中有一些使用descriptor的例子,實現很巧妙,值得一讀,前文也有介紹。references;http://www.bottlepy.org/docs/dev/https://raw.githubusercontent.com/bottlepy/bottle/master/bottle.pyhttp://blog.csdn.net/huithe/article/details/8087645http://simple-is-better.com/news/59http://www.bottlepy.org/docs/dev/deployment.html#server-optionshttp://blog.rutwick.com/use-bottle-python-framework-with-google-app-engine
python bottle 簡介