Issue 1098618: socket.setdefaulttimeout() breaks smtplib.starttls() (original) (raw)
If you use socket.setdefaulttimeout() to set a socket timeout, and then do smtplib.starttls(), no data is read after TLS is started.
Here's a session that's as you'd expect:
import smtplib s=smtplib.SMTP("smtp.example.com") s.set_debuglevel(1) s.helo() send: 'helo mint-julep.mondoinfo.com\r\n' reply: '250 smtpout-1.example.net\r\n' reply: retcode (250); Msg: smtpout-1.example.net (250, 'smtpout-1.example.net') s.starttls() send: 'STARTTLS\r\n' reply: '220 Ready to start TLS\r\n' reply: retcode (220); Msg: Ready to start TLS (220, 'Ready to start TLS') s.ehlo() send: 'ehlo mint-julep.mondoinfo.com\r\n' reply: '250-smtpout-1.example.net\r\n' reply: '250-PIPELINING\r\n' reply: '250-SIZE 32768000\r\n' reply: '250-VRFY\r\n' reply: '250-ETRN\r\n' reply: '250-AUTH LOGIN PLAIN DIGEST-MD5\r\n' reply: '250-AUTH=LOGIN PLAIN DIGEST-MD5\r\n' reply: '250 8BITMIME\r\n' reply: retcode (250); Msg: smtpout-1.example.net PIPELINING SIZE 32768000 VRFY ETRN AUTH LOGIN PLAIN DIGEST-MD5 AUTH=LOGIN PLAIN DIGEST-MD5 8BITMIME (250, 'smtpout-1.example.net\nPIPELINING\nSIZE 32768000\nVRFY\nETRN\nAUTH LOGIN PLAIN DIGEST- MD5\nAUTH=LOGIN PLAIN DIGEST-MD5\n8BITMIME') s.quit() send: 'quit\r\n' reply: '221 Bye\r\n' reply: retcode (221); Msg: Bye
But if I set a socket timeout, I get this:
import socket socket.setdefaulttimeout(30) s=smtplib.SMTP("smtp.example.com") s.set_debuglevel(1) s.helo() send: 'helo mint-julep.mondoinfo.com\r\n' reply: '250 smtpout-1.example.net\r\n' reply: retcode (250); Msg: smtpout-1.example.net (250, 'smtpout-1.example.net') s.starttls() send: 'STARTTLS\r\n' reply: '220 Ready to start TLS\r\n' reply: retcode (220); Msg: Ready to start TLS (220, 'Ready to start TLS') s.ehlo() send: 'ehlo mint-julep.mondoinfo.com\r\n' ^CTraceback (most recent call last): File "", line 1, in ? File "/Library/Frameworks/Python.framework/Versions/2.4/ lib/python2.4/smtplib.py", line 391, in ehlo (code,msg)=self.getreply() File "/Library/Frameworks/Python.framework/Versions/2.4/ lib/python2.4/smtplib.py", line 345, in getreply line = self.file.readline() File "/Library/Frameworks/Python.framework/Versions/2.4/ lib/python2.4/smtplib.py", line 160, in readline chr = self.sslobj.read(1) KeyboardInterrupt
(I waited ten or fifteen seconds before typing the control-c.)
If I set the socket timeout back to None, it works correctly:
socket.setdefaulttimeout(None) s=smtplib.SMTP("smtp.example.com") s.helo() (250, 'smtpout-2.example.net') s.starttls() (220, 'Ready to start TLS') s.ehlo() (250, 'smtpout-2.example.net\nPIPELINING\nSIZE 32768000\nVRFY\nETRN\nAUTH LOGIN PLAIN DIGEST- MD5\nAUTH=LOGIN PLAIN DIGEST-MD5\n8BITMIME') s.quit()