久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用帶前導(dǎo)斜杠的路徑的燒瓶路線

Flask route using path with leading slash(使用帶前導(dǎo)斜杠的路徑的燒瓶路線)
本文介紹了使用帶前導(dǎo)斜杠的路徑的燒瓶路線的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試使用帶有路徑轉(zhuǎn)換器的簡單路由來獲取 Flask:

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

除非 URL 的路徑"部分使用前導(dǎo)斜杠,否則它會起作用.在這種情況下,我得到一個(gè) 404.我理解該錯(cuò)誤,但我沒有得到的是文檔中或 Internet 上的任何地方都沒有關(guān)于如何解決此問題的解決方法.我覺得我是第一個(gè)嘗試做這個(gè)基本事情的人.

有沒有辦法讓它與有意義的 URL 一起工作?比如這種請求:

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

解決方案

PathConverter URL 轉(zhuǎn)換器 明確不包含前導(dǎo)斜杠;這是故意的,因?yàn)榇蠖鄶?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 選項(xiàng);如果您安裝了 Werkzeug 1.0.0 或更新版本并將其保留為默認(rèn)值,則多個(gè)連續(xù)的 / 字符將合并為一個(gè).

注冊轉(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測試 Python 代碼時(shí),我應(yīng)該如何驗(yàn)證日志消息?)
Patch __call__ of a function(修補(bǔ)函數(shù)的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對象的模擬方法中調(diào)用 self?)
Mocking only a single method on an object(僅模擬對象上的單個(gè)方法)
Mocking a subprocess call in Python(在 Python 中模擬子進(jìn)程調(diào)用)
Checking call order across multiple mocks(檢查多個(gè)模擬的調(diào)用順序)
主站蜘蛛池模板: 韩日一区二区三区 | 日韩一区二区三区在线观看 | 青青草视频网 | 自拍 亚洲 欧美 老师 丝袜 | 一级免费在线视频 | 国产高清精品一区二区三区 | 日韩激情在线 | 国产亚洲精品美女久久久久久久久久 | 欧洲一区在线观看 | 自拍 亚洲 欧美 老师 丝袜 | 欧美日韩在线精品 | 麻豆精品久久 | 国产av毛片 | 日本色综合 | 日韩在线视频一区 | 日韩在线看片 | 丁香婷婷成人 | 精品一区二区三区入口 | 国产欧美三区 | 91免费看片| 五十女人一级毛片 | 99久久久99久久国产片鸭王 | 欧美精品二区 | 欧美影院 | 免费成人毛片 | 91视视频在线观看入口直接观看 | 欧美精品在线观看 | 美日韩免费 | 99热激情 | 亚洲精品国产第一综合99久久 | 精品亚洲一区二区三区 | 国产成人综合在线 | 麻豆亚洲| 成人在线视频一区 | 亚洲视频中文字幕 | 天天操妹子 | 国产成人精品久久久 | 精品国产精品国产偷麻豆 | 欧美a区| 国产高清亚洲 | 99riav3国产精品视频 |