summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1999-03-29 20:33:21 (GMT)
committerGuido van Rossum <guido@python.org>1999-03-29 20:33:21 (GMT)
commitf123f84f66bded5a5554f7130cf823d53d3304cd (patch)
treefa6b7707fc3420d7515837de4f5685471ca61b66 /Lib
parent9065ea36deb5812654c1959413a92cfb18ad3b5a (diff)
downloadcpython-f123f84f66bded5a5554f7130cf823d53d3304cd.zip
cpython-f123f84f66bded5a5554f7130cf823d53d3304cd.tar.gz
cpython-f123f84f66bded5a5554f7130cf823d53d3304cd.tar.bz2
Patch by Per Cederqvist, who writes:
""" - It needlessly used the makefile() method for each response that is read from the SMTP server. - If the remote SMTP server closes the connection unexpectedly the code raised an IndexError. It now raises an SMTPServerDisconnected exception instead. - The code now checks that all lines in a multiline response actually contains an error code. """ The Dragon approves.
Diffstat (limited to 'Lib')
-rwxr-xr-xLib/smtplib.py21
1 files changed, 15 insertions, 6 deletions
diff --git a/Lib/smtplib.py b/Lib/smtplib.py
index 47e1faf..bd221dc 100755
--- a/Lib/smtplib.py
+++ b/Lib/smtplib.py
@@ -187,21 +187,30 @@ class SMTP:
- server response string corresponding to response code (multiline
responses are converted to a single, multiline string).
+
+ Raises SMTPServerDisconnected if end-of-file is reached.
"""
resp=[]
- self.file = self.sock.makefile('rb')
+ if self.file is None:
+ self.file = self.sock.makefile('rb')
while 1:
line = self.file.readline()
+ if line == '':
+ self.close()
+ raise SMTPServerDisconnected("Connection unexpectedly closed")
if self.debuglevel > 0: print 'reply:', `line`
resp.append(string.strip(line[4:]))
code=line[:3]
- #check if multiline resp
+ # Check that the error code is syntactically correct.
+ # Don't attempt to read a continuation line if it is broken.
+ try:
+ errcode = string.atoi(code)
+ except ValueError:
+ errcode = -1
+ break
+ # Check if multiline response.
if line[3:4]!="-":
break
- try:
- errcode = string.atoi(code)
- except(ValueError):
- errcode = -1
errmsg = string.join(resp,"\n")
if self.debuglevel > 0: