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

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

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

問題描述

我正在嘗試使用帶有路徑轉(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)!

【網(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 代碼時,我應(yīng)該如何驗證日志消息?)
Patch __call__ of a function(修補函數(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(僅模擬對象上的單個方法)
Mocking a subprocess call in Python(在 Python 中模擬子進(jìn)程調(diào)用)
Checking call order across multiple mocks(檢查多個模擬的調(diào)用順序)
主站蜘蛛池模板: 精品动漫一区 | 国产精品视频久久久久久 | 毛片免费在线观看 | 能看的av | 久久久亚洲| 能看的av网站 | 在线观看国产三级 | 欧美日韩精品一区二区三区视频 | 在线观看国产精品一区二区 | 国产精品免费看 | 亚州av在线 | 天天干,夜夜操 | 日本久久久影视 | 亚洲 欧美 日韩在线 | 欧产日产国产精品v | 久久久久久国产精品 | 中文字幕一区二区三区四区五区 | 国产免费又色又爽又黄在线观看 | 国产在线视频一区二区董小宛性色 | 91久久国产精品 | 激情五月综合 | 精品久久久久一区二区国产 | 中文字幕亚洲视频 | 超碰在线国产 | 亚洲一区二区三区桃乃木香奈 | 午夜视频免费在线观看 | 亚洲一区二区三区国产 | 在线一区二区观看 | 亚洲美女网站 | 一本色道久久综合亚洲精品高清 | 日本欧美在线 | 久久天天躁狠狠躁夜夜躁2014 | 久久久久久久久久久蜜桃 | 亚洲成人观看 | 亚洲精品一区二区 | 一级黄片一级毛片 | 色婷婷综合网 | 性国产丰满麻豆videosex | 国产免费一区二区 | 日韩欧美三区 | 成人在线视频看看 |