从python3.9.5升级到3.11.1 原来用poplib收取邮件的脚本运行失败:
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)
运行发现已经能进一步了,但是又报错误:
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)
此为自签证书错误,借处理爬虫时,忽略网站证书的方法,查找对应功能调整为:文章来源:https://www.toymoban.com/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版本下恢复正常,问题解决。文章来源地址https://www.toymoban.com/news/detail-517741.html
到了这里,关于ssl.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] 错误处理的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!