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

Flask route using path with leading slash(使用带前导斜杠的路径的烧瓶路线)
本文介绍了使用带前导斜杠的路径的烧瓶路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用带有路径转换器的简单路由来获取 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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)