問題描述
我正在嘗試使用帶有路徑轉(zhuǎn)換器的簡單路由來獲取 Flask:
@api.route('/records/<hostname>/<metric>/<path:context>')
除非 URL 的路徑"部分使用前導(dǎo)斜杠,否則它會起作用.在這種情況下,我得到一個 404.我理解該錯誤,但我沒有得到的是文檔中或 Internet 上的任何地方都沒有關(guān)于如何解決此問題的解決方法.我覺得我是第一個嘗試做這個基本事情的人.
有沒有辦法讓它與有意義的 URL 一起工作?比如這種請求:
http://localhost:5000/api/records/localhost/disks.free//dev/disk0s2
PathConverter
URL 轉(zhuǎn)換器 明確不包含前導(dǎo)斜杠;這是故意的,因為大多數(shù)路徑應(yīng)該不包含這樣的斜線.
查看 PathConverter
源代碼:
regex = '[^/].*?'
此表達(dá)式匹配任何內(nèi)容,只要它不以 /
開頭.
您不能對路徑進(jìn)行編碼;如果不是所有服務(wù)器在傳遞 URL 路徑之前都解碼它到 WSGI 服務(wù)器上.
您必須使用不同的轉(zhuǎn)換器:
導(dǎo)入 werkzeug從 werkzeug.routing 導(dǎo)入 PathConverter從包裝導(dǎo)入版本# merge_slashes 是否可用且為真MERGES_SLASHES = version.parse(werkzeug.__version__) >= version.parse("1.0.0")類 EverythingConverter(PathConverter):正則表達(dá)式 = '.*?'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 或更新版本并將其保留為默認(rèn)值,則多個連續(xù)的 /
字符將合并為一個.
注冊轉(zhuǎn)換器必須在 Flask app
對象上完成,不能在藍(lán)圖上完成.
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.
這篇關(guān)于使用帶前導(dǎo)斜杠的路徑的燒瓶路線的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!