Web框架 — Bottle

Bottle是一个快速、简洁、轻量级的基于WSIG的微型Web框架。说微型,是因为它只有一个.py文件(源码在这里可以下载,中文官方文档在这里),除Python标准库外,其不依赖于任何第三方模块。

它提供了 Python Web开发中需要的基本支持:URL路由,Request/Response对象封装,模板支持,与WSGI服务器集成支持。可以适配各种web服务器,包括python自带的wsgiref(默认),gevent, cherrypy,gunicorn等等。

Bottle适用于小型的Web开发,在应用程序规模比较小的情况下可以实现快速开发。但是由于自身功能所限,对于大型的Web程序,Bottle的功能略显不足,程序员需要手动管理模块、数据库、配置等等,与Pylons等框架相比Bottle的优势就难以体现出来了。

下载安装

pip install bottle
easy_install bottle
apt-get install python-bottle
wget http://bottlepy.org/bottle.py

Bottle框架大致可以分为以下部分:

  • Routing(路由系统):将不同请求交由指定函数处理
  • Templates(模板系统):将模板中的特殊语法渲染成字符串,值得一说的是Bottle的模板引擎可以任意指定:Bottle内置模板、makojinja2cheetah
  • Utilities(公共组件):用于提供处理请求相关的信息,如:表单数据、上传文件、 cookies、HTTP头信息和其它 HTTP相关的元数据。
  • Server(服务):内置HTTP开发服务器,并且支持 paste, fapws3, bjoern, Google App Engine, Cherrypy 或者其它任何WSGI HTTP 服务器。
     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 }
    Bottle支持的服务

 示例:框架的基本使用

from bottle import template, Bottle
root = Bottle()
 
@root.route('/hello/')
def index():
    return "Hello World"
    # return template('<b>Hello {{name}}</b>!', name="halo")
 
root.run(host='localhost', port=8080)

 路由系统

路由系统是的url对应指定函数,当用户请求某个url时,就由指定函数处理当前请求,对于Bottle的路由系统可以分为一下几类:

  • 静态路由
  • 动态路由
  • 请求方法路由
  • 二级路由

1. 静态路由

@root.route('/hello/')
def index():
    return template('<b>Hello {{name}}</b>!', name="halo")

2. 动态路由

@root.route('/wiki/<pagename>')
def callback(pagename):
    ...
 
@root.route('/object/<id:int>')
def callback(id):
    ...
 
@root.route('/show/<name:re:[a-z]+>')
def callback(name):
    ...
 
@root.route('/static/<path:path>')
def callback(path):
    return static_file(path, root='static')

3. 请求方法路由

@root.route('/hello/', method='POST')
def index():
    ...
 
@root.get('/hello/')
def index():
    ...
 
@root.post('/hello/')
def index():
    ...
 
@root.put('/hello/')
def index():
    ...
 
@root.delete('/hello/')
def index():
    ...

4. 二级路由

1 from bottle import template, Bottle
2 
3 app01 = Bottle()
4 
5 @app01.route('/hello/', method='GET')
6 def index():
7     return template('<b>App01</b>!')
app01.py
1 from bottle import template, Bottle
2 
3 app02 = Bottle()
4 
5 
6 @app02.route('/hello/', method='GET')
7 def index():
8     return template('<b>App02</b>!')
app02.py
from bottle import template, Bottle
from bottle import static_file
root = Bottle()
 
@root.route('/hello/')
def index():
    return template('<b>Root {{name}}</b>!', name="halo")
 
from framwork_bottle import app01
from framwork_bottle import app02
 
root.mount('app01', app01.app01)
root.mount('app02', app02.app02)
 
root.run(host='localhost', port=8080)

模板系统

模板系统用于将Html和自定的值两者进行渲染,从而得到字符串,然后将该字符串返回给客户端。我们知道在Bottle中可以使用 内置模板系统、makojinja2cheetah等,以内置模板系统为例: 

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <h1>{{name}}</h1>
 9 </body>
10 </html>
hello_template.html
from bottle import template, Bottle
root = Bottle()
 
@root.route('/hello/')
def index():
    # 默认情况下去目录:['./', './views/']中寻找模板文件 hello_template.html
    # 配置在 bottle.TEMPLATE_PATH 中
    return template('hello_template.tpl', name='halo')
 
root.run(host='localhost', port=8080)

1. 语法

<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 == "Alex":
        name="seven"
%>
 
<h1>4. Python、Html混合</h1>
 
% if True:
    <span>{{name}}</span>
% end
<ul>
  % for item in name:
    <li>{{item}}</li>
  % end
</ul>

2. 函数 

include(sub_template, **variables)

# 导入其他模板文件
% include('header.html', title='Page Title')
Page Content
% include('footer.tpl')

rebase(name, **variables)

1 <html>
2 <head>
3   <title>{{title or 'No title'}}</title>
4 </head>
5 <body>
6   {{!base}}
7 </body>
8 </html>
base.html
# 导入母版
 % rebase('base.tpl', title='Page Title')
<p>Page Content ...</p>

defined(name)

# 检查当前变量是否已经被定义,已定义True,未定义False

get(name, default=None)

# 获取某个变量的值,不存在时可设置默认值

setdefault(name, default)

# 如果变量不存在时,为变量设置默认值

扩展:自定义函数

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <h1>自定义函数</h1>
 9     {{ halo() }}
10 
11 </body>
12 </html>
hello_template.html
 1 from bottle import template, Bottle,SimpleTemplate
 2 root = Bottle()
 3 
 4 def custom():
 5     return '123123'
 6 
 7 @root.route('/hello/')
 8 def index():
 9     # 默认情况下去目录:['./', './views/']中寻找模板文件 hello_template.html
10     # 配置在 bottle.TEMPLATE_PATH 中
11     return template('hello_template.html', name='Halo', halo=custom)
12 
13 root.run(host='localhost', port=8080)
main.py

注:变量或函数前添加 【 ! 】,则会关闭转义的功能

公共组件

由于Web框架就是用来【接收用户请求】-> 【处理用户请求】-> 【响应相关内容】,对于具体如何处理用户请求,开发人员根据用户请求来进行处理,而对于接收用户请求和相应相关的内容均交给框架本身来处理,其处理完成之后将产出交给开发人员和用户。

【接收用户请求】: 当框架接收到用户请求之后,将请求信息封装在Bottle的request中,以供开发人员使用

【响应相关内容】: 当开发人员的代码处理完用户请求之后,会将其执行内容相应给用户,相应的内容会封装在Bottle的response中,然后再由框架将内容返回给用户

所以,公共组件本质其实就是为开发人员提供接口,使其能够获取用户信息并配置响应内容。

1. request

Bottle中的request其实是一个LocalReqeust对象,其中封装了用户请求的相关信息:

request.headers    请求头信息
 
request.query    get请求信息
 
request.forms    post请求信息
 
request.files    上传文件信息
 
request.params    get和post请求信息
 
request.GET    get请求信息
 
request.POST    post和上传信息
 
request.cookies    cookie信息
     
request.environ    环境相关相关

2. response

Bottle中的request其实是一个LocalResponse对象,其中框架即将返回给用户的相关信息:

response.status_line    状态行
 
response.status_code    状态码
 
response.headers    响应头
 
response.charset    编码

response.set_cookie    在浏览器上设置cookie
         
response.delete_cookie    在浏览器上删除cookie

实例:

 1 from bottle import route, request
 2 
 3 @route('/login')
 4 def login():
 5     return '''
 6         <form action="/login" method="post">
 7             Username: <input name="username" type="text" />
 8             Password: <input name="password" type="password" />
 9             <input value="Login" type="submit" />
10         </form>
11     '''
12 
13 @route('/login', method='POST')
14 def do_login():
15     username = request.forms.get('username')
16     password = request.forms.get('password')
17     if check_login(username, password):
18         return "<p>Your login information was correct.</p>"
19     else:
20         return "<p>Login failed.</p>"
基于form请求
 1 <form action="/upload" method="post" enctype="multipart/form-data">
 2   Category:      <input type="text" name="category" />
 3   Select a file: <input type="file" name="upload" />
 4   <input type="submit" value="Start upload" />
 5 </form>
 6 
 7 
 8 @route('/upload', method='POST')
 9 def do_upload():
10     category   = request.forms.get('category')
11     upload     = request.files.get('upload')
12     name, ext = os.path.splitext(upload.filename)
13     if ext not in ('.png','.jpg','.jpeg'):
14         return 'File extension not allowed.'
15 
16     save_path = get_save_path_for_category(category)
17     upload.save(save_path) # appends upload.filename automatically
18     return 'OK'
文件上传

 

服务

对于Bottle框架其本身未实现类似于Tornado自己基于socket实现Web服务,所以必须依赖WSGI,默认Bottle已经实现并且支持的WSGI有:

 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 }
Bottle支持的服务

使用时,只需在主app执行run方法时指定参数即可:

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,如果想要使用其他时,则需要首先安装相关类库,然后才能使用。如:

 1 # 如果使用Tornado的服务,则需要首先安装tornado才能使用
 2 
 3 class TornadoServer(ServerAdapter):
 4     """ The super hyped asynchronous server by facebook. Untested. """
 5     def run(self, handler): # pragma: no cover
 6         # 导入Tornado相关模块
 7         import tornado.wsgi, tornado.httpserver, tornado.ioloop
 8         container = tornado.wsgi.WSGIContainer(handler)
 9         server = tornado.httpserver.HTTPServer(container)
10         server.listen(port=self.port,address=self.host)
11         tornado.ioloop.IOLoop.instance().start()
bottle.py源码

 

PS:以上WSGI中提供了19种,如果想要使期支持其他服务,则需要扩展Bottle源码来自定义一个ServerAdapter

更多参见:http://www.bottlepy.org/docs/dev/index.html

 

本文大部分内容转自:http://www.cnblogs.com/wupeiqi/articles/5341480.html

转载于:https://www.cnblogs.com/yzls/articles/9545369.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值