gpt4 book ai didi

python - 带有 URI 编码组件的 flask 路由

转载 作者:IT老高 更新时间:2023-10-28 20:53:48 33 4
gpt4 key购买 nike

Flask 似乎不支持带有 URI 编码组件的路由。我很好奇我是否做错了什么,或者是否需要包含一个特殊的标志。

我的路线看起来像这样:

@app.route('/foo/<encoded>/bar/')
def foo(encoded):
# ...
pass

应该匹配的 URL 如下所示:

http://foobar.com/foo/xxx/bar/ # matched correctly, no URI component
http://foobar.com/foo/x%2Fx%2Fx%2F/bar/ # not matched correctly, URI component

前一个 URL 有效,后者吐出一个可爱的 404。

谢谢!

最佳答案

添加 path到您的网址规则:

@app.route('/foo/<path:encoded>/bar/')

根据评论更新:路由 API 文档在这里:http://flask.pocoo.org/docs/api/#flask.Flask.route .实现 path 的底层类样式路由转换器在这里:http://werkzeug.pocoo.org/docs/routing/#custom-converters (这是 pocoostan 非常棒的部分之一。)就尾部斜线而言,有一些特殊的规则相当于:

If a rule ends with a slash and is requested without a slash by the user, the user is automatically redirected to the same page with a trailing slash attached.

If a rule does not end with a trailing slash and the user request the page with a trailing slash, a 404 not found is raised.

另外请记住,如果您使用的是 Apache 并且期待一个以斜杠结尾的 url,即提交给 http://ex.com/foo/<path:encoded>/bar 的书签。和 encoded得到带有双斜杠的东西,Apache 会将多个斜杠转换为一个。

关于python - 带有 URI 编码组件的 flask 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7100573/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com