bottle
Bottle是一个快速、简洁、轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 pip install bottle 2 easy_install bottle 3 apt-get install python-bottle 4 wget http://bottlepy.org/bottle.py
Bottle框架大致可以分为以下部分:
- 路由系统,将不同请求交由指定函数处理
- 模板系统,将模板中的特殊语法渲染成字符串,值得一说的是Bottle的模板引擎可以任意指定:Bottle内置模板、mako、jinja2、cheetah
- 公共组件,用于提供处理请求相关的信息,如:表单数据、cookies、请求头等
- 服务,Bottle默认支持多种基于WSGI的服务
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 server_names = { 2 'cgi': CGIServer, 3 'flup': FlupFCGIServer, 4 'wsgiref': WSGIRefServer, 5 'waitress': WaitressServer, 6 'cherrypy': CherryPyServer, 7 'paste': PasteServer, 8 'fapws3': FapwsServer, 9 'tornado': TornadoServer, 10 'gae': AppEngineServer, 11 'twisted': TwistedServer, 12 'diesel': DieselServer, 13 'meinheld': MeinheldServer, 14 'gunicorn': GunicornServer, 15 'eventlet': EventletServer, 16 'gevent': GeventServer, 17 'geventSocketIO':GeventSocketIOServer, 18 'rocket': RocketServer, 19 'bjoern' : BjoernServer, 20 'auto': AutoServer, 21 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 from bottle import template, Bottle 4 root = Bottle() 5 6 @root.route('/hello/') 7 def index(): 8 return "Hello World" 9 # return template('<b>Hello {{name}}</b>!', name="xxx") 10 11 root.run(host='localhost', port=8080)
一、路由系统
路由系统是的url对应指定函数,当用户请求某个url时,就由指定函数处理当前请求,对于Bottle的路由系统可以分为一下几类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @root.route('/hello/') 2 def index(): 3 return template('<b>Hello {{name}}</b>!', name="xxx")
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @root.route('/wiki/<pagename>') 2 def callback(pagename): 3 ... 4 5 @root.route('/object/<id:int>') 6 def callback(id): 7 ... 8 9 @root.route('/show/<name:re:[a-z]+>') 10 def callback(name): 11 ... 12 13 @root.route('/static/<path:path>') 14 def callback(path): 15 return static_file(path, root='static')
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @root.route('/hello/', method='POST') 2 def index(): 3 ... 4 5 @root.get('/hello/') 6 def index(): 7 ... 8 9 @root.post('/hello/') 10 def index(): 11 ... 12 13 @root.put('/hello/') 14 def index(): 15 ... 16 17 @root.delete('/hello/') 18 def index(): 19 ...
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # app01.py 2 3 #!/usr/bin/env python 4 # -*- coding:utf-8 -*- 5 from bottle import template, Bottle 6 7 app01 = Bottle() 8 9 @app01.route('/hello/', method='GET') 10 def index(): 11 return template('<b>App01</b>!') 12 13 14 15 16 #app02.py 17 18 #!/usr/bin/env python 19 # -*- coding:utf-8 -*- 20 from bottle import template, Bottle 21 22 app02 = Bottle() 23 24 25 @app02.route('/hello/', method='GET') 26 def index(): 27 return template('<b>App02</b>!') 28 29 30 31 32 33 #!/usr/bin/env python 34 # -*- coding:utf-8 -*- 35 from bottle import template, Bottle 36 from bottle import static_file 37 root = Bottle() 38 39 @root.route('/hello/') 40 def index(): 41 return template('<b>Root {{name}}</b>!', name="xxx") 42 43 from framwork_bottle import app01 44 from framwork_bottle import app02 45 46 root.mount('app01', app01.app01) 47 root.mount('app02', app02.app02) 48 49 root.run(host='localhost', port=8080)
二、模板系统
模板系统用于将Html和自定的值两者进行渲染,从而得到字符串,然后将该字符串返回给客户端。我们知道在Bottle中可以使用 内置模板系统、mako、jinja2、cheetah等,以内置模板系统为例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <h1>{{name}}</h1> </body> </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 from bottle import template, Bottle 4 root = Bottle() 5 6 @root.route('/hello/') 7 def index(): 8 # 默认情况下去目录:['./', './views/']中寻找模板文件 a.html 9 # 配置在 bottle.TEMPLATE_PATH 中 10 return template('a.html', name='xxx') 11 12 root.run(host='localhost', port=8888)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
''' 单值 单行Python代码 Python代码快 Python、Html混合 ''' <h1>1、单值</h1> {{name}} <h1>2、单行Python代码</h1> % s1 = "hello" <h1>3、Python代码块</h1> <% # A block of python code name = name.title().strip() if name == "xxx": name="seven" %> <h1>4、Python、Html混合</h1> % if True: <span>{{name}}</span> % end <ul> % for item in name: <li>{{item}}</li> % end </ul>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
include(sub_template, **variables) # 导入其他模板文件 % include('header.html', title='Page Title') Page Content % include('footer.html') rebase(name, **variables) <html> <head> <title>{{title or 'No title'}}</title> </head> <body> {{!base}} </body> </html> # 导入母版 % rebase('base.html', title='Page Title') <p>Page Content ...</p> defined(name) # 检查当前变量是否已经被定义,已定义True,未定义False get(name, default=None) # 获取某个变量的值,不存在时可设置默认值 setdefault(name, default) # 如果变量不存在时,为变量设置默认值 # 自定义 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <h1>自定义函数</h1> {{ xxx() }} </body> </html> #!/usr/bin/env python # -*- coding:utf-8 -*- from bottle import template, Bottle,SimpleTemplate root = Bottle() def custom(): return '123123' @root.route('/hello/') def index(): # 默认情况下去目录:['./', './views/']中寻找模板文件 a.html # 配置在 bottle.TEMPLATE_PATH 中 return template('a.html', name='alex', xxx=custom) root.run(host='localhost', port=8080) # 变量或函数前添加 【 ! 】,则会关闭转义的功能
三、公共组件
由于Web框架就是用来【接收用户请求】-> 【处理用户请求】-> 【响应相关内容】,对于具体如何处理用户请求,开发人员根据用户请求来进行处理,而对于接收用户请求和相应相关的内容均交给框架本身来处理,其处理完成之后将产出交给开发人员和用户。
【接收用户请求】
当框架接收到用户请求之后,将请求信息封装在Bottle的request中,以供开发人员使用
【响应相关内容】
当开发人员的代码处理完用户请求之后,会将其执行内容相应给用户,相应的内容会封装在Bottle的response中,然后再由框架将内容返回给用户
所以,公共组件本质其实就是为开发人员提供接口,使其能够获取用户信息并配置响应内容。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # Bottle中的request其实是一个LocalReqeust对象,其中封装了用户请求的相关信息: 2 3 request.headers 4 # 请求头信息 5 6 request.query 7 # get请求信息 8 9 request.forms 10 # post请求信息 11 12 request.files 13 # 上传文件信息 14 15 request.params 16 # get和post请求信息 17 18 request.GET 19 # get请求信息 20 21 request.POST 22 # post和上传信息 23 24 request.cookies 25 # cookie信息 26 27 request.environ 28 # 环境相关相关
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # Bottle中的request其实是一个LocalResponse对象,其中框架即将返回给用户的相关信息: 2 3 response 4 response.status_line 5 # 状态行 6 7 response.status_code 8 # 状态码 9 10 response.headers 11 # 响应头 12 13 response.charset 14 # 编码 15 16 response.set_cookie 17 # 在浏览器上设置cookie 18 19 response.delete_cookie 20 # 在浏览器上删除cookie
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from bottle import route, request @route('/login') def login(): return ''' <form action="/login" method="post"> Username: <input name="username" type="text" /> Password: <input name="password" type="password" /> <input value="Login" type="submit" /> </form> ''' @route('/login', method='POST') def do_login(): username = request.forms.get('username') password = request.forms.get('password') if check_login(username, password): return "<p>Your login information was correct.</p>" else: return "<p>Login failed.</p>"
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<form action="/upload" method="post" enctype="multipart/form-data"> Category: <input type="text" name="category" /> Select a file: <input type="file" name="upload" /> <input type="submit" value="Start upload" /> </form> @route('/upload', method='POST') def do_upload(): category = request.forms.get('category') upload = request.files.get('upload') name, ext = os.path.splitext(upload.filename) if ext not in ('.png','.jpg','.jpeg'): return 'File extension not allowed.' save_path = get_save_path_for_category(category) upload.save(save_path) # appends upload.filename automatically return 'OK'
四、服务
# 对于Bottle框架其本身未实现类似于Tornado自己基于socket实现Web服务,所以必须依赖WSGI,默认Bottle已经实现并且支持的WSGI server_names = { 'cgi': CGIServer, 'flup': FlupFCGIServer, 'wsgiref': WSGIRefServer, 'waitress': WaitressServer, 'cherrypy': CherryPyServer, 'paste': PasteServer, 'fapws3': FapwsServer, 'tornado': TornadoServer, 'gae': AppEngineServer, 'twisted': TwistedServer, 'diesel': DieselServer, 'meinheld': MeinheldServer, 'gunicorn': GunicornServer, 'eventlet': EventletServer, 'gevent': GeventServer, 'geventSocketIO':GeventSocketIOServer, 'rocket': RocketServer, 'bjoern' : BjoernServer, 'auto': AutoServer, } # 使用时,只需在主app执行run方法时指定参数即可 #!/usr/bin/env python # -*- coding:utf-8 -*- from bottle import Bottle root = Bottle() @root.route('/hello/') def index(): return "Hello World" # 默认server ='wsgiref' root.run(host='localhost', port=8080, server='wsgiref') # 默认server="wsgiref",即:使用Python内置模块wsgiref,如果想要使用其他时,则需要首先安装相关类库,然后才能使用。 #bottle.py源码 # 如果使用Tornado的服务,则需要首先安装tornado才能使用 class TornadoServer(ServerAdapter): """ The super hyped asynchronous server by facebook. Untested. """ def run(self, handler): # pragma: no cover # 导入Tornado相关模块 import tornado.wsgi, tornado.httpserver, tornado.ioloop container = tornado.wsgi.WSGIContainer(handler) server = tornado.httpserver.HTTPServer(container) server.listen(port=self.port,address=self.host) tornado.ioloop.IOLoop.instance().start() #以上WSGI中提供了19种,如果想要使期支持其他服务,则需要扩展Bottle源码来自定义一个ServerAdapter http://www.bottlepy.org/docs/dev/index.html