python - 将包含斜杠的参数传递给 Bottle

标签 python bottle

我需要将包含斜杠的字符串通过 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

关于python - 将包含斜杠的参数传递给 Bottle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19767484/

相关文章:

python - 生成器表达式 Python

python拆分空字符串

python - 如何通过 bottle 默认提供 index.html?

python - BottlyPy - 如何阅读 UWSGI_SCHEME?

python - 获取字典/json中键的类型

python - 有什么理由不使用 SQLObject 而不是 SQLAlchemy?

python - Django 如何验证用户是否已经存在

javascript - 使用 Bottle.py 加载 CSS 和 JavaScript

python - 使用 Python Bottle 的 Webhelpers

python - before_request 的响应