• python bottle学习(三)动态路由配置(通配符)


    from bottle import (run, route, get, post,
                        default_app, Bottle)
    
    
    @route('/', method='GET')
    @route('/index')
    def hello():
        return 'hello, word!'
    
    """
    bottle的动态route提供了五种默认的类型,即:
    str,int,float,path和re(正则),格式为:<name:filter>
    """
    
    """如果route中没有指定类型,只有变量名,默认是str
    但是你不能指定str,如:/api/test/<name:str>,这样写
    会报错,如果是str类型的直接留一个变量名就好,不需要
    加上:str,如下
    """
    @route('/api/test/<n>')
    def hello(n):
        print type(n)
        return '--str---'+n
    
    # 匹配int
    @route('/api/test/<n:int>')
    def hello(n):
        print type(n)
        return '--int---'+str(n)
    
    # 匹配float
    @route('/api/test/<n:float>')
    def hello(n):
        print type(n)
        return '--float---'+str(n)
    
    # 匹配path
    @route('/api/test/<n:path>')
    def hello(n):
        print type(n)
        return '--path---'+str(n)
    
    # 匹配正则
    @route('/api/test/<n:re:.+>')
    def hello(n):
        print type(n)
        return '--path---'+str(n)
    
    
    # 另外,python支持自定义filter,官方的例子:
    官方文档
    def list_filter(config):
        ''' Matches a comma separated list of numbers. '''
        delimiter = config or ','
        regexp = r'd+(%sd)*' % re.escape(delimiter)
    
        def to_python(match):
            return map(int, match.split(delimiter))
    
        def to_url(numbers):
            return delimiter.join(map(str, numbers))
    
        return regexp, to_python, to_url
    
    app.router.add_filter('list', list_filter)
    
    @route('/follow/<ids:list>')
    def follow_users(ids):
        。。。
    if __name__ == '__main__': run(host='0.0.0.0', port=1234, reloader=True)
  • 相关阅读:
    android-为应用单元测试
    android手机拨号器实现
    android模拟器使用
    android开发环境搭建
    C语言之关键字
    linux shell脚本基础-----3
    linux shell脚本基础-----2
    linux shell脚本基础-----1
    Android学习计划
    MySql 绿色版配置
  • 原文地址:https://www.cnblogs.com/dadong616/p/6840767.html
Copyright © 2020-2023  润新知