我想用Python监视使用HTTPS的网站。
问题是网站上的证书无效。
我只想知道我不在乎网站的运行。在
我的工作代码如下:from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
req = Request("https://somedomain.com")
try:
response = urlopen(req)
except HTTPError as e:
print('server couldn\'t fulfill the request')
print('error code: ', e.code)
except URLError as e:
print(e.args)
else:
print ('website ok')
最后调用URLError。错误代码是645。在
^{pr2}$
所以,我试着把代码645排除在外。我试过了:from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
req = Request("https://somedomain.com")
try:
response = urlopen(req)
except HTTPError as e:
print('server couldn\'t fulfill the request')
print('error code: ', e.code)
except URLError as e:
if e.code == 645:
print("ok")
print(e.args)
else:
print ('website ok')
但是得到这个错误:Traceback (most recent call last):
File "monitor443.py", line 11, in
if e.code == 645:
AttributeError: 'URLError' object has no attribute 'code'
如何添加此异常?在