使用带前导斜杠的路径的烧瓶路线

2022-01-08 00:00:00 python flask routes

问题描述

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

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

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

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

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

解决方案

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

查看 PathConverter 源代码:

<块引用>

regex = '[^/].*?'

此表达式匹配任何内容,只要它不以 / 开头.

您不能对路径进行编码;如果不是所有服务器在传递 URL 路径之前都解码它到 WSGI 服务器上.

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

导入 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.

相关文章