python bottle 簡介

python bottle 簡介

目錄

     

    正文

      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.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)

        下面是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服務器,以及各自的特性。

        

    NameHomepageDescription
    cgi
    Run as CGI script
    flupflupRun as FastCGI process
    gaegaeHelper for Google App Engine deployments
    wsgirefwsgirefSingle-threaded default server
    cherrypycherrypyMulti-threaded and very stable
    pastepasteMulti-threaded, stable, tried and tested
    rocketrocketMulti-threaded
    waitresswaitressMulti-threaded, poweres Pyramid
    gunicorngunicornPre-forked, partly written in C
    eventleteventletAsynchronous framework with WSGI support.
    geventgeventAsynchronous (greenlets)
    dieseldieselAsynchronous (greenlets)
    fapws3fapws3Asynchronous (network side only), written in C
    tornadotornadoAsynchronous, powers some parts of Facebook
    twistedtwistedAsynchronous, well tested but... twisted
    meinheldmeinheldAsynchronous, partly written in C
    bjoernbjoernAsynchronous, 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)。

     


    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章