• python bottle框架


    python bottle框架

    • 简介:

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

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

    路由系统,将不同请求交由指定函数处理
    模板系统,将模板中的特殊语法渲染成字符串,值得一说的是Bottle的模板引擎可以任意指定:Bottle内置模板、mako、jinja2、cheetah
    公共组件,用于提供处理请求相关的信息,如:表单数据、cookies、请求头等
    服务,Bottle默认支持多种基于WSGI的服务

    • 安装

    pip install bottle

    easy_install bottle

    框架基本使用,例:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    from bottle import Bottle
    root = Bottle()
     
    @root.route('/index/')
    def index():
        return "Hello World"
     
    root.run(host='localhost', port=8080)
    

     效果:

    一、路由系统

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

    静态路由
    动态路由, 正则表达式
    请求方法路由, POST、GET、PUT等
    二级路由, 分发至其它入口

    1、静态路由

    @root.route('/index/')
    def index():
        return "welcome index page"
    

    2、动态路由

    输入的URL跟参数

    @root.route('/index/<pagename>')
    def index(pagename):
        return pagename
    

    输入的参数为数字

    @root.route('/index/<id:int>')
    def index(id):
        return str(id)
    

     

    正则表达式

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

    静态文件

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    from bottle import Bottle, static_file
    root = Bottle()
    
    @root.route('/index/<path:path>')
    def index(path):
        return static_file(path, root='E:static')
    
    root.run(host='localhost', port=8080)
    

     3、请求方法路由

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

     4、二级路由

    index.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from bottle import template, Bottle
    from bottle import static_file
    
    root = Bottle()
    
    @root.route('/index/')
    def index():
        return template('<b>Root {{name}}</b>!', name="wang")
    
    import sub_index01
    import sub_index02
    
    root.mount('index01', sub_index01.index01_obj)
    root.mount('index02', sub_index02.index02_obj)
    
    root.run(host='localhost', port=8080)
    

    sub_index01.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from bottle import Bottle
    
    index01_obj = Bottle()
    
    @index01_obj.route('/sub_index01/', method='GET')
    def index():
        return "sub_index01"
    

    sub_index02.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from bottle import Bottle
    
    index02_obj = Bottle()
    
    @index02_obj.route('/sub_index02/', method='GET')
    def index():
        return "sub_index02"
    

    访问主页

    访问sub_index01子应用和sub_index02子应用

  • 相关阅读:
    「CF1380G」 Circular Dungeon
    「CF1208G」 Polygons
    P4827「国家集训队」 Crash 的文明世界
    「CF85E」 Guard Towers
    「BZOJ 2956」模积和
    「HEOI2016/TJOI2016」排序
    CF277E Binary Tree on Plane
    「SDOI2016」数字配对
    HNOI2020「Elegy」
    CSP-S2019「Symphony」
  • 原文地址:https://www.cnblogs.com/shhnwangjian/p/5637759.html
Copyright © 2020-2023  润新知