從python3.9.5升級到3.11.1 原來用poplib收取郵件的腳本運行失?。?/p>
server = poplib.POP3_SSL(pop3_server, 995)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Python311-32\Lib\poplib.py", line 452, in __init__
POP3.__init__(self, host, port, timeout)
File "c:\Python311-32\Lib\poplib.py", line 104, in __init__
self.sock = self._create_socket(timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Python311-32\Lib\poplib.py", line 456, in _create_socket
sock = self.context.wrap_socket(sock,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Python311-32\Lib\ssl.py", line 517, in wrap_socket
return self.sslsocket_class._create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Python311-32\Lib\ssl.py", line 1075, in _create
self.do_handshake()
File "c:\Python311-32\Lib\ssl.py", line 1346, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:992)
查看python升級變化的文檔,提示在3.10版有涉及:
Important deprecations, removals or restrictions:
PEP 644, Require OpenSSL 1.1.1 or newer
應該是ssl相關的版本變化引起。
為使新版的SSL與原服務器的適配,增加設置:
import ssl
ctx = ssl.create_default_context()
ctx.set_ciphers('DEFAULT')
#...
server = poplib.POP3_SSL(pop3_server, 995,context=ctx)
運行發(fā)現(xiàn)已經(jīng)能進一步了,但是又報錯誤:
server = poplib.POP3_SSL(pop3_server, 995,context=ctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\python311-32\Lib\poplib.py", line 452, in __init__
POP3.__init__(self, host, port, timeout)
File "C:\python311-32\Lib\poplib.py", line 104, in __init__
self.sock = self._create_socket(timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\python311-32\Lib\poplib.py", line 456, in _create_socket
sock = self.context.wrap_socket(sock,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\python311-32\Lib\ssl.py", line 517, in wrap_socket
return self.sslsocket_class._create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\python311-32\Lib\ssl.py", line 1075, in _create
self.do_handshake()
File "C:\python311-32\Lib\ssl.py", line 1346, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:992)
此為自簽證書錯誤,借處理爬蟲時,忽略網(wǎng)站證書的方法,查找對應功能調(diào)整為:文章來源:http://www.zghlxwxcb.cn/news/detail-517741.html
import ssl
ctx = ssl._create_unverified_context() #起到忽略證書校驗的作用
ctx.set_ciphers('DEFAULT') #與老服務器握手搭配
#...
server = poplib.POP3_SSL(pop3_server, 995,context=ctx)
至此,在新python3.11版本下恢復正常,問題解決。文章來源地址http://www.zghlxwxcb.cn/news/detail-517741.html
到了這里,關于ssl.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] 錯誤處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!