gpt4 book ai didi

python - urlparse 不会为无效的 url 引发异常

转载 作者:太空宇宙 更新时间:2023-11-03 23:59:48 25 4
gpt4 key购买 nike

我有以下代码,用于检查输入的 url 是否有效:

#!/usr/bin/env python3

import sys
import urllib.parse

# ...

def checkValidURL(someURL):
try:
parsed_url = urllib.parse.urlparse(someURL)
isURL = True
except ValueError:
print("Invalid URL!")
sys.exit(0)

# ...

if __name__ == "__main__":
checkValidURL(someURL)

如果输入了无效的 URL,例如:someURL="http://ijfjiör@@@a:43244434::" 它应该引发一个 ValueError 作为描述 here :

Characters in the netloc attribute that decompose under NFKC normalization (as used by the IDNA encoding) into any of /, ?, #, @, or : will raise a ValueError. If the URL is decomposed before parsing, no error will be raised.

但是,没有出现异常,而且 URL 似乎是有效的。

我有什么地方做错了吗?还有其他方法可以检查 URL 的有效性吗?

最佳答案

您的 URL 不会分解为包含禁止字符的字符串,因此引用在这里完全不相关。

引用中的语言严格禁止使用 internationalized domain name encodinghttp://xn--foo/ 生成类似 http://?/ 的东西,因为你在这里没有这样做,所以没有 ValueError 是生成的或确实是预期的。

(抱歉,不是在我可以创建真实工作示例的地方。)

关于python - urlparse 不会为无效的 url 引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56064086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com