西安站,在哪里做推广效果好,排名优化公司哪里有,wordpress 导航加图标bottle是一个轻量级的pythonweb框架#xff0c; 可以适配各种web服务器#xff0c;包括python自带的wsgiref(默认)#xff0c;gevent#xff0c; cherrypy#xff0c;gunicorn等等。bottle是单文件形式发布#xff0c;源码在这里可以下载#xff0c;代码量不多#xff…bottle是一个轻量级的pythonweb框架 可以适配各种web服务器包括python自带的wsgiref(默认)gevent cherrypygunicorn等等。bottle是单文件形式发布源码在这里可以下载代码量不多可以用来学习web框架。这里也有官方文档的中文翻译。 首先我们来运行一下bottle的hello worldfrom bottle importrunif __name__ __main__:defapplication(environ, start_response):start_response(200 OK, [(Content-Type, text/html)])return [Hello world!]run(hostlocalhost, port8080, appapplication)上面的代码看起来也非常符合wsgi的接口规范。启动改代码可以看到输出 Bottle v0.13-dev server starting up (usingWSGIRefServer())… Listening on http://localhost:8080/ Hit Ctrl-C to quit. 输出中加粗部分表明使用的web服务器是python自带的wsgiref。也可以使用其他web server比如gevent前提是需要安装gevent修改后的代码如下from bottle importrunimportgevent.monkeygevent.monkey.patch_all()if __name__ __main__:defapplication(environ, start_response):start_response(200 OK, [(Content-Type, text/html)])return [Hello world!]run(hostlocalhost, port8080, appapplication, server gevent)通过server关键字指定web服务器为‘gevent’输出的第一行变成了Bottle v0.13-dev server starting up (usingGeventServer())… 不管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, _castdefwsgi(self, environ, start_response):The bottle WSGI-interface.try:outself._cast(self._handle(environ))#rfc2616 section 4.3if 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返回更加丰富的类型比如dictFile等。 _cast函数对_handle函数返回值进行处理使之符合wsgi规范bottle.Route 封装了路由规则与对应的回调bottle.RouterA 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服务器以及各自的特性。NameHomepageDescriptioncgiRun as CGI scriptflupRun as FastCGI processgaeHelper for Google App Engine deploymentswsgirefSingle-threaded default servercherrypyMulti-threaded and very stablepasteMulti-threaded, stable, tried and testedrocketMulti-threadedwaitressMulti-threaded, poweres PyramidgunicornPre-forked, partly written in CeventletAsynchronous framework with WSGI support.geventAsynchronous (greenlets)dieselAsynchronous (greenlets)fapws3Asynchronous (network side only), written in CtornadoAsynchronous, powers some parts of FacebooktwistedAsynchronous, well tested but… twistedmeinheldAsynchronous, partly written in CbjoernAsynchronous, very fast and written in CautoAutomatically selects an available server adapter可以看到bottle适配的web服务器很丰富。工作模式也很全面有多线程的(如paste)、有多进程模式的(如gunicorn)、也有基于协程的(如gevent)。具体选择哪种web服务器取决于应用的特性比如是CPU bound还是IO boundbottle.run 启动wsgi服务器。几个比较重要的参数 app wsgi application即可以是bottle.Bottle 也开始是任何满足wsgi 接口的函数 server wsgi http server字符串 hostport 监听端口 核心逻辑 ServerAdapter.run(app)。 最后bottle源码中有一些使用descriptor的例子实现很巧妙值得一读前文也有介绍。 referenceshttp://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