mirror of
https://github.com/python/cpython.git
synced 2025-12-07 17:57:56 +00:00
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.
This commit is contained in:
parent
9065ea36de
commit
f123f84f66
1 changed files with 15 additions and 6 deletions
|
|
@ -187,21 +187,30 @@ class SMTP:
|
||||||
|
|
||||||
- server response string corresponding to response code (multiline
|
- server response string corresponding to response code (multiline
|
||||||
responses are converted to a single, multiline string).
|
responses are converted to a single, multiline string).
|
||||||
|
|
||||||
|
Raises SMTPServerDisconnected if end-of-file is reached.
|
||||||
"""
|
"""
|
||||||
resp=[]
|
resp=[]
|
||||||
|
if self.file is None:
|
||||||
self.file = self.sock.makefile('rb')
|
self.file = self.sock.makefile('rb')
|
||||||
while 1:
|
while 1:
|
||||||
line = self.file.readline()
|
line = self.file.readline()
|
||||||
|
if line == '':
|
||||||
|
self.close()
|
||||||
|
raise SMTPServerDisconnected("Connection unexpectedly closed")
|
||||||
if self.debuglevel > 0: print 'reply:', `line`
|
if self.debuglevel > 0: print 'reply:', `line`
|
||||||
resp.append(string.strip(line[4:]))
|
resp.append(string.strip(line[4:]))
|
||||||
code=line[:3]
|
code=line[:3]
|
||||||
#check if multiline resp
|
# Check that the error code is syntactically correct.
|
||||||
if line[3:4]!="-":
|
# Don't attempt to read a continuation line if it is broken.
|
||||||
break
|
|
||||||
try:
|
try:
|
||||||
errcode = string.atoi(code)
|
errcode = string.atoi(code)
|
||||||
except(ValueError):
|
except ValueError:
|
||||||
errcode = -1
|
errcode = -1
|
||||||
|
break
|
||||||
|
# Check if multiline response.
|
||||||
|
if line[3:4]!="-":
|
||||||
|
break
|
||||||
|
|
||||||
errmsg = string.join(resp,"\n")
|
errmsg = string.join(resp,"\n")
|
||||||
if self.debuglevel > 0:
|
if self.debuglevel > 0:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue