web框架--bottle 安装 bottle介绍 基本使用1234567891011#!/usr/bin/env python# -*- coding:utf-8 -*-from bottle import template, Bottleroot = Bottle() @root.route('/hello/')def index(): return "Hello World" # return template('Hello {{name}}!', name="Alex") root.run(host='localhost', port=8080)具体介绍一、路由系统 具体介绍一、路由系统
bottle框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块
pip install bottle
easy_install bottle
apt
-
get install python
-
bottle
wget http:
/
/
bottlepy.org
/
bottle.py
bottle介绍
Bottle是一个快速、简洁、轻量级的基于WSIG的微型Web框架
Bottle框架大致可以分为以下部分:
路由系统,将不同请求交由指定函数处理
模板系统,将模板中的特殊语法渲染成字符串,值得一说的是Bottle的模板引擎可以任意指定:Bottle内置模板、mako、jinja2、cheetah
公共组件,用于提供处理请求相关的信息,如:表单数据、cookies、请求头等
服务,Bottle默认支持多种基于WSGI的服务,默认使用的wsgiref,如果想使用其他服务,先安装
基本使用
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from
bottle
import
template, Bottle
root
=
Bottle()
@root
.route(
'/hello/'
)
def
index():
return
"Hello World"
# return template('<b>Hello {{name}}</b>!', name="Alex")
root.run(host
=
'localhost'
, port
=
8080
)
具体介绍一、路由系统
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from
bottle
import
template, Bottle
root
=
Bottle()
@root
.route(
'/hello/'
)
def
index():
return
"Hello World"
# return template('<b>Hello {{name}}</b>!', name="Alex")
root.run(host
=
'localhost'
, port
=
8080
)
一、路由系统
路由系统是的url对应指定函数,当用户请求某个url时,就由指定函数处理当前请求,路由可以有多个route对应同一个函数。
分类:
静态路由
动态路由
请求方法路由
二级路由
1、静态路由(最普通的,写死的)
例如,基本使用里的 @root
.route(
'/hello/'
)
2、动态路由(根据正则表达式变化)
@root
.route(
'/wiki/<pagename>'
) #<>号是规定格式,pagename默认是字符串类型
def
callback(pagename):
...
@root
.route(
'/object/<id:int>'
) #相当于(d+)
def
callback(
id
):
...
@root
.route(
'/show/<name:re:[a-z]+>'
) #正则匹配
def
callback(name):
...
@root
.route(
'/static/<path:path>'
) #path可以匹配多个‘/’
def
callback(path):
return
static_file(path, root
=
'static'
)
3、请求方法路由(getpostdeletegetputoptionhead.....)
@root
.route(
'/hello/'
, method
=
'POST'
)
def
index():
...
@root
.get(
'/hello/'
) #代表只接收get请求
def
index():
...
@root
.post(
'/hello/'
)
def
index():
...
@root
.put(
'/hello/'
)
def
index():
...
@root
.delete(
'/hello/'
)
def
index():
...
4、二级路由(相当于Django的include,将xx开头的请求转发到某个app里)
在bottle里,所有文件都是app,但是分等级关系,所有请求来了都会由root(Bottle对象)这个app去处理,根据请求再分发给其它app。
下例中,root是根,当请求以app01开头,就转发到app01.py中的app01对象来处理。
index.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from
bottle
import
template, Bottle
from
bottle
import
static_file
root
=
Bottle()
@root
.route(
'/hello/'
)
def
index():
return
template(
'<b>Root {{name}}</b>!'
, name
=
"Alex"
)
from
framwork_bottle
import
app01
from
framwork_bottle
import
app02
root.mount(
'app01'
, app01.app01) #挂载之后,app01开头的都会交有app01.app01对象去处理
root.mount(
'app02'
, app02.app02)
root.run(host
=
'localhost'
, port
=
8080
)
app01.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from
bottle
import
template, Bottle
app01
=
Bottle()
@app01
.route(
'/hello/'
, method
=
'GET'
)
def
index():
return
template(
'<b>App01</b>!'
)
app01.py
(app02.py内容同app01)
二、模板系统
模板系统用于将Html和自定的值两者进行渲染,从而得到字符串,然后将该字符串返回给客户端。
Bottle中可以使用 内置模板系统、mako、jinja2、cheetah等,以内置模板系统为例:
hello_template.tpl
<!DOCTYPE html>
<
html
>
<
head
lang
=
"en"
>
<
meta
charset
=
"UTF-8"
>
<
title
></
title
>
</
head
>
<
body
>
<
h1
>{{name}}</
h1
>
</
body
>
</
html
>
hello_template.tpl
index.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
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
=
'alex'
)
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>
#相当于jinja2的{% for ... %}
%
if
True
:
<span>{{name}}<
/
span>
%
end
<ul>
%
for
item
in
name:
<li>{{item}}<
/
li>
%
end
<
/
ul>
2、函数
include(sub_template, **variables)
# 导入其他模板文件
%
include(
'header.tpl'
, title
=
'Page Title'
)
Page Content
%
include(
'footer.tpl'
)
rebase(name, **variables) --母版继承
base.tpl
<html>
<head>
<title>{{title
or
'No title'
}}<
/
title>
<
/
head>
<body>
{{!base}}
<
/
body>
<
/
html>
base.tpl
son.html
# 导入母版
%
rebase(
'base.tpl'
, title
=
'Page Title'
)
<p>Page Content ...<
/
p>
defined(name)
# 获取某个变量的值,不存在时可设置默认值
setdefault(name, default)
# 如果变量不存在时,为变量设置默认值
扩展:自定义函数(略)
三、公共组件
由于Web框架就是用来【接收用户请求】-> 【处理用户请求】-> 【响应相关内容】,对于具体如何处理用户请求,开发人员根据用户请求来进行处理,而对于接收用户请求和相应相关的内容均交给框架本身来处理,其处理完成之后将产出交给开发人员和用户。
1、接收用户请求:request
当框架接收到用户请求之后,将请求信息封装在Bottle的request中,以供开发人员使用
Bottle中的request其实是一个LocalReqeust对象,其中封装了用户请求的相关信息:
request.headers
HTTP
请求头信息 (ip,浏览器......)
request.query
get
请求信息 /?page=123
request.forms
post请求信息
request.files
上传文件信息
request.params
get
和post请求信息
request.GET
get
请求信息
request.POST
post和上传信息
request.cookies
cookie信息
request.environ
环境相关相关 #上面没有的都在这里
2、响应相关内容:response
当开发人员的代码处理完用户请求之后,会将其执行内容相应给用户,相应的内容会封装在Bottle的response中,然后再由框架将内容返回给用户
Bottle中的request其实是一个LocalResponse对象,其中框架即将返回给用户的相关信息:
response
response.status_line
状态行
response.status_code
状态码
response.headers
响应头
response.charset
编码
response.set_cookie
在浏览器上设置cookie
response.delete_cookie
在浏览器上删除cookie
所以,公共组件本质其实就是为开发人员提供接口,使其能够获取用户信息并配置响应内容。
四、服务
对于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,如果想要使用其他时,则需要首先安装相关类库,然后才能使用。如:
# 如果使用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()
PS:以上WSGI中提供了19种,如果想要使期支持其他服务,则需要扩展Bottle源码来自定义一个ServerAdapter
更多参见:http://www.bottlepy.org/docs/dev/index.html