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

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

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

問(wèn)題描述

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

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

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

有沒(méi)有辦法讓它與有意義的 URL 一起工作?比如這種請(qǐng)求:

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

解決方案

PathConverter URL 轉(zhuǎn)換器 明確不包含前導(dǎo)斜杠;這是故意的,因?yàn)榇蠖鄶?shù)路徑應(yīng)該包含這樣的斜線(xiàn).

查看 PathConverter 源代碼:

<塊引用>

regex = '[^/].*?'

此表達(dá)式匹配任何內(nèi)容,只要它不以 / 開(kāi)頭.

您不能對(duì)路徑進(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")類(lèi) 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è).

注冊(cè)轉(zhuǎn)換器必須在 Flask app 對(duì)象上完成,不能在藍(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)斜杠的路徑的燒瓶路線(xiàn)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測(cè)試 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 中對(duì)象的模擬方法中調(diào)用 self?)
Mocking only a single method on an object(僅模擬對(duì)象上的單個(gè)方法)
Mocking a subprocess call in Python(在 Python 中模擬子進(jìn)程調(diào)用)
Checking call order across multiple mocks(檢查多個(gè)模擬的調(diào)用順序)
主站蜘蛛池模板: 日韩黄色小视频 | 成年人黄色 | 欧美性猛交乱大交 | 欧美一级片 | 欧美日韩一区在线 | 免费av大片 | 欧洲精品一区二区 | 特黄aaaaaaaaa真人毛片 | 中文字幕在线一区二区三区 | 国产成人午夜高潮毛片 | 亚洲精品系列 | 亚洲激情视频 | 日韩 欧美 亚洲 | 午夜激情在线观看 | 午夜激情影视 | 婷婷天堂| 性视频在线| 天天干天天操 | aa一级片| 一道本在线视频 | 国产黄色一区二区 | 一区二区三区免费观看 | 国产伦精品一区二区三区在线 | 日韩精品三区 | 国产精品国产三级国产专区53 | 日韩免费 | 久久精品日韩 | 国产视频a | 91亚洲国产成人久久精品网站 | 久久久亚洲精品视频 | 超碰在线免费 | 伊人久久免费视频 | 日韩在线专区 | 日韩专区在线观看 | 亚洲综合精品 | 黄大色黄大片女爽一次 | 国产精品免费一区 | www亚洲| 国产一区二区不卡 | 色就是色欧美 | 亚洲一级在线 |