我想用Python来监视使用HTTPS的网站。 问题是,网站上的证书无效。 我不在乎,我只是想知道网站正在运行。python capture URLError code
我工作的代码如下所示:
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.
C:\python>python monitor443.py
(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)'),)
所以,我试图除了代码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 <module>
if e.code == 645:
AttributeError: 'URLError' object has no attribute 'code'
如何添加此异常?
['URLError'是OSERROR的一个子类(http://stackoverflow.com/q/38773209/344286)可能会说,它是在['e.errno'发现](https://docs.python.org/3/library/exceptions.html#OSError.errno)? –
e.errno返回“none” – Shawn
另外... 645不是errorno,那是错误发生的C源代码行,AFAIK。 –