Bottle web framework

13 篇文章 0 订阅

1. 安装

Bottle不依赖任何外部库或者模块,只需要下载 bottle.py 文件到项目中然后像引用一般模块一样引用即可

$ wget https://bottlepy.org/bottle.py

其他途径:pip (recommended), easy_install or your package manager:

$ sudo pip install bottle              # recommended
$ sudo easy_install bottle             # alternative without pip
$ sudo apt-get install python-bottle   # works for debian, ubuntu, ...

2. hello world

目录结构如下:

bottle模块文件(bottle.py),应用程序文件(runapp.py),模板文件目录(views)

这里写图片描述

我们的hello world程序:

app = Bottle()

@app.route('/hello')
def hello():
    return "Hello World!"

run(app, host='0.0.0.0', port=8080)

然后python runapp.py运行程序
浏览器中输入http://ip:8080就可以看到

Hello World!

3. 使用模板进行渲染

在程序中引用模板:

from bottle import template

主程序:

@route('/')
@route('/hello/<name>')
def greet(name='Stranger'):
    return template('Hello {{name}}, how are you?', name=name)

4. 动态路由

@route('/wiki/<pagename>')            # matches /wiki/Learning_Python
def show_wiki_page(pagename):
    ...

@route('/<action>/<user>')            # matches /follow/defnull
def user_api(action, user):
    ...

最后,来个稍微有点感觉的演示程序:

功能点:表单传值,cookie,模板文件使用,post方式下json传值,文件读写,重定向

import os
from bottle import route, run, request, response, redirect, view

@route('/index')
@view('index')
#模板文件引用装饰器,Bottle will look for templates in the ./views/ folder or any folder specified in the bottle.TEMPLATE_PATH list.
def index():
    username = request.get_cookie("account", secret='166f3437-ab33-443b-ad59-0e543c51a11c')
    return dict(username=username)#向index模板传值

@route('/logout')
def logout():
    response.set_cookie("account", '' )#删除cookie值
    redirect('/login')

@route('/login')
def login():
    username = request.get_cookie("account", secret='166f3437-ab33-443b-ad59-0e543c51a11c')#获取cookie
    if username:
        redirect('/index')#重定向
    else:
        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():
    def check_login(username, password):
        if username  and password:
            return True
        return False

    username = request.forms.get('username')
    password = request.forms.get('password')
    if check_login(username, password):
        response.set_cookie("account", username, secret='166f3437-ab33-443b-ad59-0e543c51a11c')#设置cookie
        redirect('/index')
    else:
        redirect('/login')

@route('/api/read')#需要在url中传递path值
def remote_read():
    data = ''
    path = request.query.path
    if os.path.exists(path):
        with open(path,'r')as fp:
            data = fp.read()
    return data

@route('/api/write', method='POST')
#ajax post请求,数据为json格式
def remote_write():
    data = request.json
    path = data['path']
    content = data['content']
    with open(path,'w')as fp:
        fp.write(content)
    return ({"code":0,'message':"success"})

run(host='0.0.0.0', port=8089, debug=True)

登录:
这里写图片描述
登录成功:
这里写图片描述

写文件:
这里写图片描述

读文件:
这里写图片描述
更多内容见官网:https://bottlepy.org/docs/dev/tutorial.html#installation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值