使用带前导斜杠的路径的 Flask 路由 [英] Flask route using path with leading slash

查看:26
本文介绍了使用带前导斜杠的路径的 Flask 路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有路径转换器的简单路由来获取 Flask:

@api.route('/records///')

除非 URL 的路径"部分使用前导斜杠,否则它会起作用.在这种情况下,我得到 404.我理解错误,但我不明白的是文档中或 Internet 上的任何地方都没有关于如何解决此问题的解决方法.我觉得我是第一个尝试做这个基本事情的人.

有没有办法让它与有意义的 URL 一起工作?例如这种请求:

http://localhost:5000/api/records/localhost/disks.free//dev/disk0s2

解决方案

PathConverter URL 转换器 明确不包含前导斜杠;这是故意的,因为大多数路径不应该包含这样的斜线.

请参阅

> 源代码:

<块引用>

regex = '[^/].*?'

该表达式匹配任何内容,前提是它不以 / 开头.

您无法对路径进行编码;如果不是所有服务器都在传递 URL 路径之前解码 URL 路径,那么尝试通过 URL 将它们编码为 %2F 来使路径中的斜杠不是 URL 分隔符而是值的一部分到 WSGI 服务器.

您必须使用不同的转换器:

import werkzeug从 werkzeug.routing 导入 PathConverter从包装进口版本# merge_slashes 是否可用且为真MERGES_SLASHES = version.parse(werkzeug.__version__) >= version.parse("1.0.0")类 EverythingConverter(PathConverter):正则表达式 = '.*?'app.url_map.converters['everything'] = EverythingConverterconfig = {"merge_slashes": False} if MERGES_SLASHES else {}@api.route('/records/<hostname>/<metric>/<everything:context>', **config)

注意 merge_slashes 选项;如果您安装了 Werkzeug 1.0.0 或更新版本并保留默认设置,则多个连续的 / 字符将合并为一个.

必须在 Flask app 对象上注册转换器,不能在蓝图上注册.

I am trying to get Flask using a simple route with a path converter:

@api.route('/records/<hostname>/<metric>/<path:context>') 

It works unless the "path" part of the URL uses a leading slash. In this case I get a 404. I understand the error but what I don't get is that there is no workaround in the documentation or anywhere on the Internet about how to fix this. I feel like I am the first one trying to do this basic thing.

Is there a way to get this working with meaningful URL? For example this kind of request:

http://localhost:5000/api/records/localhost/disks.free//dev/disk0s2 

解决方案

The PathConverter URL converter explicitly doesn't include the leading slash; this is deliberate because most paths should not include such a slash.

See the PathConverter source code:

regex = '[^/].*?'

This expression matches anything, provided it doesn't start with /.

You can't encode the path; attempting to make the slashes in the path that are not URL delimiters but part of the value by URL-encoding them to %2F doesn't fly most, if not all servers decode the URL path before passing it on to the WSGI server.

You'll have to use a different converter:

import werkzeug
from werkzeug.routing import PathConverter
from packaging import version

# whether or not merge_slashes is available and true
MERGES_SLASHES = version.parse(werkzeug.__version__) >= version.parse("1.0.0")

class EverythingConverter(PathConverter):
    regex = '.*?'

app.url_map.converters['everything'] = EverythingConverter

config = {"merge_slashes": False} if MERGES_SLASHES else {}
@api.route('/records/<hostname>/<metric>/<everything:context>', **config) 

Note the merge_slashes option; if you have Werkzeug 1.0.0 or newer installed and leave this at the default, then multiple consecutive / characters are collapsed into one.

Registering a converters must be done on the Flask app object, and cannot be done on a blueprint.

这篇关于使用带前导斜杠的路径的 Flask 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆