python bottle 簡介
阿新 • • 發佈:2017-05-15
安裝 unicorn net __name__ gre stc home 路由 test bottle 是一個輕量級的python web框架, 可以適配各種web服務器,包括python自帶的wsgiref(默認),gevent, cherrypy,gunicorn等等。bottle是單文件形式發布,源碼在這裏可以下載,代碼量不多,可以用來學習web框架。這裏也有官方文檔的中文翻譯。
首先我們來運行一下bottle的hello world
上面的代碼看起來也非常符合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,修改後的代碼如下:
下面是Bottle.wsgi函數的核心代碼,主要調用兩個比較重要的函數:_handle, _cast
可以看到,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.py
http://blog.csdn.net/huithe/article/details/8087645
http://simple-is-better.com/news/59
http://www.bottlepy.org/docs/dev/deployment.html#server-options
http://blog.rutwick.com/use-bottle-python-framework-with-google-app-engine
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)
from bottle import run import gevent.monkey gevent.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)
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 |
python bottle 簡介