python bottle支持https_python bottle 简介
bottle是一個(gè)輕量級(jí)的python
web框架, 可以適配各種
web服務(wù)器,包括python自帶的wsgiref(默認(rèn)),gevent, cherrypy,gunicorn等等。bottle是單文件形式發(fā)布,源碼在
這里可以下載,代碼量不多,可以用來(lái)學(xué)習(xí)web框架。
這里也有官方文檔的中文翻譯。 ? 首先我們來(lái)運(yùn)行一下bottle的hello world
from bottle importrunif __name__ == '__main__':defapplication(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])return ['
Hello world!
']run(host='localhost', port=8080, app=application)
上面的代碼看起來(lái)也非常符合wsgi的接口規(guī)范。啟動(dòng)改代碼,可以看到輸出 ? ? ? ??Bottle?v0.13-dev?server?starting?up?(using
WSGIRefServer())… ? ? ? ? Listening?on?http://localhost:8080/ ? ? ? ? Hit?Ctrl-C?to?quit. ?? 輸出中加粗部分表明使用的web服務(wù)器是
python自帶的wsgiref。也可以使用其他web server,比如gevent,前提是需要安裝gevent,修改后的代碼如下:
from bottle importrunimportgevent.monkey
gevent.monkey.patch_all()if __name__ == '__main__':defapplication(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])return ['
Hello world!
']run(host='localhost', port=8080, app=application, server = 'gevent')
通過(guò)server關(guān)鍵字指定web服務(wù)器為‘gevent’,輸出的第一行變成了:
Bottle?v0.13-dev?server?starting?up?(using
GeventServer())… ? 不管bottle用什么web服務(wù)器啟動(dòng),在瀏覽器輸入127.0.0.1:8080,都可以看到
? ? ? 下面介紹bottle中部分類(lèi)和接口
bottle.Bottle ? ? 代表一個(gè)獨(dú)立的wsgi應(yīng)用,由一下部分組成:routes,?callbacks,?plugins,?resources?and?configuration。 ? ? __call__: Bottle定義了__call__函數(shù), 使得Bottle的實(shí)例能成為一個(gè)
callable。在
前文提到,web框架(或Application)需要提供一個(gè)callbale對(duì)象給web服務(wù)器,bottle提供的就是Bottle實(shí)例
def __call__(self, environ, start_response):"""Each instance of :class:'Bottle' is a WSGI application."""
return self.wsgi(environ, start_response)
下面是Bottle.wsgi函數(shù)的核心代碼,主要調(diào)用兩個(gè)比較重要的函數(shù):_handle, _cast
defwsgi(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:處理請(qǐng)求,最終調(diào)用到application?,簡(jiǎn)化后的代碼如下:
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:? ? ? ? 標(biāo)準(zhǔn)的wsgi接口對(duì)Application的返回值要求嚴(yán)格,必須迭代返回字符串。bottle做了一些擴(kuò)展,可以允許App返回更加豐富的類(lèi)型,比如dict,File等。?_cast函數(shù)對(duì)_handle函數(shù)返回值進(jìn)行處理,使之符合wsgi規(guī)范
bottle.Route ? ? 封裝了路由規(guī)則與對(duì)應(yīng)的回調(diào)
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服務(wù)器的基類(lèi),子類(lèi)只要實(shí)現(xiàn)run方法就可以了,bottle里面有大量的Web服務(wù)器的適配。下表來(lái)自官網(wǎng),介紹了bottle支持的各種web服務(wù)器,以及各自的特性。
Name
Homepage
Description
cgi
Run as CGI script
flup
Run as FastCGI process
gae
Helper for Google App Engine deployments
wsgiref
Single-threaded default server
cherrypy
Multi-threaded and very stable
paste
Multi-threaded, stable, tried and tested
rocket
Multi-threaded
waitress
Multi-threaded, poweres Pyramid
gunicorn
Pre-forked, partly written in C
eventlet
Asynchronous framework with WSGI support.
gevent
Asynchronous (greenlets)
diesel
Asynchronous (greenlets)
fapws3
Asynchronous (network side only), written in C
tornado
Asynchronous, powers some parts of Facebook
twisted
Asynchronous, well tested but… twisted
meinheld
Asynchronous, partly written in C
bjoern
Asynchronous, very fast and written in C
auto
Automatically selects an available server adapter
可以看到,bottle適配的web服務(wù)器很豐富。工作模式也很全面,有多線程的(如paste)、有多進(jìn)程模式的(如gunicorn)、也有基于協(xié)程的(如gevent)。具體選擇哪種web服務(wù)器取決于應(yīng)用的特性,比如是CPU bound還是IO bound
bottle.run ? ? 啟動(dòng)wsgi服務(wù)器。幾個(gè)比較重要的參數(shù) ? ? app: wsgi application,即可以是bottle.Bottle 也開(kāi)始是任何滿足wsgi 接口的函數(shù) ? ? server: wsgi http server,字符串 ? ? host:port: 監(jiān)聽(tīng)端口 ? ?? ? ? 核心邏輯: ? ? ServerAdapter.run(app)。 ? 最后,bottle源碼中有一些使用descriptor的例子,實(shí)現(xiàn)很巧妙,值得一讀,
前文也有介紹。 ? 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
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的python bottle支持https_python bottle 简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java从控制台输入数组_Java将控制
- 下一篇: vue开发手机页面闪烁_Vue页面加载闪