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 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)        
    下面是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)。
 
最后,bottle源码中有一些使用descriptor的例子,实现很巧妙,值得一读, 前文也有介绍。
 
references;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值