web框架--bottle

安装

bottle框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块


1

2

3

4

pip install bottle

easy_install bottle

apt-get install python-bottle

wget http://bottlepy.org/bottle.py

bottle介绍

Bottle是一个快速、简洁、轻量级的基于WSIG的微型Web框架

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

  • 路由系统,将不同请求交由指定函数处理
  • 模板系统,将模板中的特殊语法渲染成字符串,值得一说的是Bottle的模板引擎可以任意指定:Bottle内置模板、makojinja2cheetah
  • 公共组件,用于提供处理请求相关的信息,如:表单数据、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)

具体介绍

一、路由系统

路由系统是的url对应指定函数,当用户请求某个url时,就由指定函数处理当前请求,路由可以有多个route对应同一个函数。

分类:

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

1、静态路由(最普通的,写死的)

例如,基本使用里的 @root.route(‘/hello/‘)

2、动态路由(根据正则表达式变化)


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@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、请求方法路由(get\post\delete\get\put\option\head.....)


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#!/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

1

2

3

4

5

6

7

8

9

10

11

#!/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中可以使用 内置模板系统、makojinja2cheetah等,以内置模板系统为例:

hello_template.tpl

1

2

3

4

5

6

7

8

9

10

11

12

<!DOCTYPE html>

<html>

<head lang="en">

    <meta charset="UTF-8">

    <title></title>

</head>

<body>

    <h1>{{name}}</h1>

</body>

</html>

hello_template.tpl

index.py

1

2

3

4

5

6

7

8

9

10

11

12

#!/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、语法


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<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)

1

2

3

4

5

# 导入其他模板文件

 

% include(‘header.tpl‘, title=‘Page Title‘)

Page Content

% include(‘footer.tpl‘)

rebase(name, **variables)    --母版继承
base.tpl

1

2

3

4

5

6

7

8

9

10

<html>

<head>

  <title>{{title or ‘No title‘}}</title>

</head>

<body>

  {{!base}}

</body>

</html>

base.tpl

son.html

1

2

3

4

# 导入母版

 

% rebase(‘base.tpl‘, title=‘Page Title‘)

<p>Page Content ...</p>

defined(name)

1

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

setdefault(name, default)

1

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

扩展:自定义函数(略)

三、公共组件

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

1、接收用户请求:request

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

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


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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对象,其中框架即将返回给用户的相关信息:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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有:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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方法时指定参数即可:


1

2

3

4

5

6

7

8

9

10

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


1

2

3

4

5

6

7

8

9

10

11

# 如果使用Tornado的服务,则需要首先安装tornado才能使用

# bottle.py源码

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

来自为知笔记(Wiz)

时间: 2024-08-27 02:35:29

web框架--bottle的相关文章

轻量的web框架Bottle

简洁的web框架Bottle 简介 Bottle是一个非常简洁,轻量web框架,与django形成鲜明的对比,它只由一个单文件组成,文件总共只有3700多行代码,依赖只有python标准库.但是麻雀虽小五脏俱全,基本的功能都有实现,很适合做一些小的web应用 开始使用 首先使用pip install bottle安装然后是一个官方文档中的例子: from bottle import route, run @route('/hello') def hello(): return "Hello Wo

微型 Python Web 框架 Bottle

Bottle 是一个非常小巧但高效的微型 Python Web 框架, 它被设计为仅仅只有一个文件的Python模块, 并且除Python标准库外, 它不依赖于任何第三方模块. 路由(Routing): 将请求映射到函数, 可以创建十分优雅的 URL 模板(Templates): Pythonic 并且快速的 Python 内置模板引擎, 同时还支持 mako, jinja2, cheetah 等第三方模板引擎 工具集(Utilites): 快速的读取 form 数据, 上传文件, 访问 coo

python 简单web框架: Bottle

基本映射 映射使用在根据不同URLs请求来产生相对应的返回内容.Bottle使用route() 修饰器来实现映射. 1 2 3 4 5 from bottle import route, [email protected]('/hello')def hello():     return "Hello World!"run() # This starts the HTTP server 运行这个程序,访问http://localhost:8080/hello将会在浏览器里看到 &quo

Python之路【第十八篇】:Web框架们

Python之路[第十八篇]:Web框架们 Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. 1 2 3 4 pip install bottle easy_install bottle apt-get install python-bottle wget http://bottlepy.org/bottle.py Bottle框架大致可以分为以下部分: 路

Django:之不得不说的web框架们

python的web框架 Bottle Bpttle是一个快速.简洁.轻量级的基于WSIG的微型web框架,此框架只有一个.py文件,除了python的标准库外,其不依赖任何其它模块. pip install bottle easy_install bottle apt-get install python-bottle wget http://bottlepy.org/bottle.py Bottle框架大致可以分为以下部分: 路由系统,将不同请求交由指定函数处理 模版系统,将模版中的特殊语法

Web框架们

Python之路[第十八篇]:Web框架们 Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. ? 1 2 3 4 pip install bottle easy_install bottle apt-get install python-bottle wget http://bottlepy.org/bottle.py Bottle框架大致可以分为以下部分:

Python 常用Web框架的比较

从GitHub中整理出的15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python Web应用开发框架 Django 应该是最出名的Python框架,GAE甚至Erlang都有框架受它影响.Django是走大而全的方向,它最出名的是其全自动化的管理后台:只需要使用起ORM,做简单的对象定义,它就能自动生成数据库结构.以及全功能的管理后台. Diesel:基于Greenlet的事件I/O框架 Diesel提供一个

微型 Python Web 框架: Bottle

微型 Python Web 框架: Bottle 在 19/09/11 07:04 PM 由 COSTONY 发表 Bottle 是一个非常小巧但高效的微型 Python Web 框架,它被设计为仅仅只有一个文件的Python模块,并且除Python标准库外,它不依赖于任何第三方模块. 路由(Routing):将请求映射到函数,可以创建十分优雅的 URL 模板(Templates):Pythonic 并且快速的 Python 内置模板引擎,同时还支持 mako, jinja2, cheetah

python bottle web框架简介

Bottle 是一个快速,简单,轻量级的 Python WSGI Web 框架.单一文件,只依赖 Python 标准库 .bottle很适合会一点python基础的人使用,因为这框架用起来很简单,只要你会python基础语法,有一点WEB知识,就可以开发出很不错的WEB.学了python的运维人员,压根不需要django框架,就可以运维工具了,毕竟django学习起来,比较复杂,学习时间也长,我们有必要一定要使用django吗? URL 映射 (Routing): 将 URL 请求映射到 Pyt