bottle框架

Bottle是一个快速、简洁、轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块。
Bottle框架的基本使用

from bottle import template, Bottle                          #去bottle里引入Bottle
root = Bottle()                                              #通过Bottle类实例化创建一个类的对象root

@root.route('/hello/')                                       #index函数被装饰器装饰了,hello是url对应了index函数
def index():
    return "欢迎西西"

root.run(host='localhost', port=8080)                        #select运行起来

访问:http://localhost:8080/hello/
返回:欢迎西西
一.Bottle框架大致可以分为以下部分:
1.路由系统:将不同请求交由指定函数处理
路由系统是的url对应指定函数,当用户请求某个url时,就由指定函数处理当前请求,对于Bottle的路由系统可以分为一下几类:
(1)静态路由:

from bottle import template, Bottle                          #去bottle里引入Bottle
root = Bottle()                                                #通过Bottle类实例化创建一个类的对象root

@root.route('/hello/')                                       #index函数被装饰器装饰了,hello是url对应了index函数
def index():
    return template('<b>你好 {{name}}</b>!',name="西西")

root.run(host='localhost', port=8080)                        #select运行起来

访问:http://localhost:8080/hello/
返回:你好 西西!
(2)动态路由
<1>

@root.route('/wiki/<pagename>')
def callback(pagename):
    ...

<2>

@root.route('/object/<id:int>')
def callback(id):
    ...

<3>

@root.route('/show/<name:re:[a-z]+>')
def callback(name):
    ...

<4>获取静态文件

from bottle import template, Bottle,static_file              #去bottle里引入static_file静态文件
root = Bottle()                                                #通过Bottle类实例化创建一个类的对象root

@root.route('/static/<path:path>')                        #访问static/加静态文件名字
def callback(path):
    return static_file(path, root='static')

root.run(host='localhost', port=8080)                        #select运行起来

访问:http://localhost:8080/static/commons.css
返回:body {
    background-color: red;
}
(3)请求方法路由
<1>POST

from bottle import template, Bottle,static_file              #去bottle里引入static_file静态文件
import bottle

#默认情况下去目录:['./', './views/']中寻找模板文件 index.html
bottle.TEMPLATE_PATH.append('./templates/')        #配置在bottle.TEMPLATE_PATH中就去templates中去找index.html

root = Bottle()                                      #通过Bottle类实例化创建一个类的对象root

@root.route('/login/',method="POST")               #POST方式访问
def login():
    return template('login.html')

root.run(host='localhost', port=8080)              #select运行起来

<2>GET

from bottle import template, Bottle,static_file              #去bottle里引入static_file静态文件
import bottle

#默认情况下去目录:['./', './views/']中寻找模板文件 index.html
bottle.TEMPLATE_PATH.append('./templates/')        #配置在bottle.TEMPLATE_PATH中就去templates中去找index.html

root = Bottle()                                      #通过Bottle类实例化创建一个类的对象root

@root.route('/login/',method="GET")               #GET方式访问
def login():
    return template('login.html')

root.run(host='localhost', port=8080)              #select运行起来

(4)二级路由
2.模板系统:将模板中的特殊语法渲染成字符串
视图(模版渲染)
-获取用户内容 request
-数据库操作
-文件操作
-返回给用户内容return ""
-模版引擎渲染
(1)引入内置模板系统
<1>bottle_kj emplatesindex.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    西西
</body>
</html>

<2>执行

from bottle import template, Bottle,static_file              #去bottle里引入static_file静态文件
import bottle

#默认情况下去目录:['./', './views/']中寻找模板文件 index.html
bottle.TEMPLATE_PATH.append('./templates/')                 #配置在bottle.TEMPLATE_PATH中就去templates中去找index.html

root = Bottle()                                                #通过Bottle类实例化创建一个类的对象root

@root.route('/hello/')                        #访问static/加静态文件名字
def callback():
    return template('index.html')

root.run(host='localhost', port=8080)                        #select运行起来

访问:http://localhost:8080/hello/
返回:西西
(2)渲染语法
<1>单值

{{name}}

<2>单行Python代码

<ul>
    % s1 = "hello"
    {{s1}}
</ul>

<3>Python代码快
<4>Python、Html混合

<ul>
    % if True:
        <span>{{name}}</span>
    % end
    % for item in name:
        <li>{{item}}</li>
     % end
</ul>

(3)函数
<1>include导入其他模版文件
bottle_kj emplates pl.html

<h1>{{title}}</h1>

bottle_kj emplatesindex.html

<body>
    <!--把参数title="西西"传到tpl.html进行渲染-->
    % include('tpl.html',title="西西")
</body>

<2>rebase模版继承
<3>defined(name)检查当前变量是否已经被定义,已定义True,未定义False
<4>get(name, default=None)获取某个变量的值,不存在时可设置默认值
<5>setdefault(name, default)如果变量不存在时,为变量设置默认值
<6>自定义函数
1)bottle_kj emplatesindex.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h1>自定义函数</h1>
    {{ xixi() }}                <!--执行bottle_kj1.py里的custom函数-->

</body>
</html>

2)bottle_kj1.py

from bottle import template, Bottle,static_file,request,redirect        #去bottle里引入request
import bottle

#默认情况下去目录:['./', './views/']中寻找模板文件 index.html
bottle.TEMPLATE_PATH.append('./templates/')        #配置在bottle.TEMPLATE_PATH中就去templates中去找index.html

root = Bottle()                                      #通过Bottle类实例化创建一个类的对象root

def custom():
    return '123456'

@root.route('/index/', method=['POST', 'GET'])
def index():
    user_list = [
        {'id':1,'name':'东东','age':18},
        {'id': 1, 'name': '南南', 'age': 18},
    ]
    return template('index.html',name='xixi',xixi=custom)

root.run(host='localhost', port=8080)       #select运行起来

3.公共组件:用于提供处理请求相关的信息
由于Web框架就是用来接收用户请求->处理用户请求->响应相关内容,对于具体如何处理用户请求,开发人员根据用户请求来进行处理,而对于接收用户请求和相应相关的内容均交给框架本身来处理,其处理完成之后将产出交给开发人员和用户。
接收用户请求:当框架接收到用户请求之后,将请求信息封装在Bottle的request中,以供开发人员使用
响应相关内容:当开发人员的代码处理完用户请求之后,会将其执行内容相应给用户,相应的内容会封装在Bottle的response中,然后再由框架将内容返回给用户
所以,公共组件本质其实就是为开发人员提供接口,使其能够获取用户信息并配置响应内容
(1)request
Bottle中的request其实是一个LocalReqeust对象,其中封装了用户请求的相关信息:
<1>请求头信息:request.headers
<2>get请求信息request.query
<3>post请求信息:request.forms
<4>上传文件信息:request.files
<5>get和post请求信息:request.params
<6>get请求信息:request.GET
<7>post和上传信息:request.POST
<8>cookie信息:request.cookies  
<9>环境相关相关:request.environ
(2)response
4.服务:对于Bottle框架其本身未实现类似于Tornado自己基于socket实现Web服务,所以必须依赖WSGI,默认Bottle已经实现并且支持的WSGI有

原文地址:https://www.cnblogs.com/xixi18/p/11002787.html