[英]urlparse does not raise exception for an invalid url
I have the following code, which is supposed to check if an entered url is valid: 我有以下代码,应该检查输入的网址是否有效:
#!/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)
If an invalid URL is entered, for example: someURL="http://ijfjiör@@@a:43244434::"
it should raise a ValueError
as described here : 如果输入无效的URL,例如:
someURL="http://ijfjiör@@@a:43244434::"
这应引起ValueError
的描述在这里 :
Characters in the netloc attribute that decompose under NFKC normalization (as used by the IDNA encoding) into any of /, ?, #, @, or : will raise a ValueError.
netloc属性中的字符在NFKC规范下(由IDNA编码使用)分解为/,?,#,@或:中的任何一个,将引发ValueError。 If the URL is decomposed before parsing, no error will be raised.
如果在解析之前将URL分解,则不会引发任何错误。
However, no exception is raised and the URL seems to be valid. 但是,不会引发任何异常,并且该URL似乎有效。
Is there anything I am doing wrong or is there any other way to check the validity of an URL? 我做错了什么吗,还是有其他方法可以检查URL的有效性?
Your URL doesn't decompose into a string which contains a prohibited character, so the quotation is not at all relevant here. 您的URL不会分解为包含禁止字符的字符串,因此引号在这里根本不相关。
The language in the quote is strictly about disallowing the use of internationalized domain name encoding like http://xn--foo/
to produce something like http://?/
and since you are not doing that here, no ValueError
is generated or indeed to be expected. 引号中的语言严格是关于禁止使用国际化域名编码(例如
http://xn--foo/
来产生类似http://?/
并且由于此处ValueError
此操作,因此不会生成ValueError
或确实是可以预期的。
(Sorry, not in a place where I can create a genuine working example.) (对不起,不在我可以创建真实的工作示例的地方。)
I'm not sure if the python validators module would be a better fit for your use case? 我不确定python验证器模块是否会更适合您的用例?
$ python3
>>>
>>>
>>> import validators
>>>
>>> validators.url("http://ijfjiör@@@a:43244434::")
ValidationFailure(func=url, args={'value': 'http://ijfjiör@@@a:43244434::', 'public': False})
>>>
>>> validators.url("http://www.google.com:8080/")
True
>>>
>>> validators.url("http://www.我愛你.com:8080/你好")
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.