Python标准库smtplib的Bug解决

此Bug出现在smtp.SMTP_SSL()类

smtp.SMTP_SSL()类的BUG

Bug产生原因

smtp.SMTP_SSL()类的BUG产生原因1
当我们在SMTP_SSL类实例化的时候直接传入host及port时,可以正常连接服务器。但如果先实例化SMTP_SSL类后,再调用connect()方法时就会报 server_hostname cannot be an empty string or start with a leading dot. 的错误

import smtplib


smtp_server = "smtp.163.com"
con1 = smtplib.SMTP_SSL(smtp_server, 465)

此Bug产生的原因是因为在SMTP_SSL类实例化的时候的时候有一个基类的初始化,如果你传了host和port,在SMTP基类里,self._host就是传入的host。但如果你先实例化再调用connect()方法,基类的self._host就是默认参数值空""。
smtp.SMTP_SSL()类的BUG产生原因2

BUG的解决

smtp.SMTP_SSL()类BUG的解决方法1
如图所示,把SMTP_SSL类_get_socket方法中的的server_hostname=self._host改成server_hostname=host即可。
smtp.SMTP_SSL()类BUG的解决方法2

猜你喜欢

转载自blog.csdn.net/weixin_41845533/article/details/87423004