我的網絡應用程序請求多個URL,並且有時會引發SSL證書錯誤。他們都是第三方網址,所以我無法修復他們的錯誤,我不想登錄他們。不過,有些東西是自己記錄下來的:2017-08-05 00:22:49,496 ERROR -- : Certificate did not match expected hostname: www.improving-autonomy.org. Certificate: {'subjectAltName': [('DNS', '*.wordpress.com'), ('DNS', 'wordpress.com')], 'subject': ((('commonName', u'*.wordpress.com'),),)}
任何人都知道我該如何阻止它?請找到我的代碼波紋管。提前謝謝了!不記錄'證書與預期的主機名不符'錯誤消息
try :
ua = UserAgent()
headers = {'Content-Type' : 'text/html', 'Accept-Encoding' : None, 'User-Agent' : ua.random}
response = requests.get(url, headers=headers, timeout=10)
except ssl.CertificateError as e :
pass
更新 - : 看起來請求模塊的日誌它(connection.py)。如果我已經捕捉到相同的異常,爲什麼它會繼續記錄?
def _match_hostname(cert, asserted_hostname):
try:
match_hostname(cert, asserted_hostname)
except CertificateError as e:
log.error(
'Certificate did not match expected hostname: %s. '
'Certificate: %s', asserted_hostname, cert
)
# Add cert to exception and reraise so client code can inspect
# the cert when catching the exception, if they want to
e._peer_cert = cert
raise
您是否嘗試通過調試器來查看日誌實際發生的位置?它看起來像也許有檢查錯誤,發現錯誤,日誌被創建,然後引發異常。需要通過追蹤代碼來驗證。 – idjaw
它發生在請求模塊中。 connection.py文件 –
任何想法?我已經遇到了同樣的例外。爲什麼它自己記錄? –