Skip to main content
 首页 » 编程设计

python之将包含斜杠的参数传递给 bottle

2024年11月01日10落叶

我需要通过 url 中的最后一个参数将包含斜杠的字符串传递到我的 bottlepy 服务器,但是由于斜杠被视为参数分隔符,服务器没有按照我需要的方式处理它。 我找到了一个关于 flask 如何支持这个的页面: http://flask.pocoo.org/snippets/76/ 但是还没有在bottle中找到类似的解决方案

请您参考如下方法:

听起来像你想要的:path :

:path matches all characters including the slash character in a non-greedy way and may be used to match more than one path segment.

例如,

@route('/root/<path:thepath>') 
def callback(thepath): 
    # `thepath` is everything after "/root/" in the URI. 
    ... 

编辑:为了回应 OP 的评论(如下),这里有一个对我有用的片段:

from bottle import Bottle, route 
 
app = Bottle() 
 
@app.route('/add/<uid>/<collection>/<group>/<items:path>') 
def add(uid, collection, group, items): 
    return 'your uri path args: {}, {}, {}, {}\n'.format(uid, collection, group, items) 
 
app.run(host='0.0.0.0', port=8081) 

产量:

% ~>curl 'http://127.0.0.1:8081/add/1/2/3/and/now/a/path' 
your uri path args: 1, 2, 3, and/now/a/path