summaryrefslogtreecommitdiffstats
path: root/Lib/ftplib.py
diff options
context:
space:
mode:
authorNadeem Vawda <nadeem.vawda@gmail.com>2011-07-23 13:51:16 (GMT)
committerNadeem Vawda <nadeem.vawda@gmail.com>2011-07-23 13:51:16 (GMT)
commitb42c53e442b211d0ded1d4c9abd18c74d29ed663 (patch)
tree0ad39ae840da96efaa2ff6bdb49f2c44a226c0aa /Lib/ftplib.py
parent578617ad453c399ea52c0aef937fdf0904b2213c (diff)
downloadcpython-b42c53e442b211d0ded1d4c9abd18c74d29ed663.zip
cpython-b42c53e442b211d0ded1d4c9abd18c74d29ed663.tar.gz
cpython-b42c53e442b211d0ded1d4c9abd18c74d29ed663.tar.bz2
Issue #10883: Fix socket leaks in urllib.request.
* ftpwrapper now uses reference counting to ensure that the underlying socket is closed when the ftpwrapper object is no longer in use * ftplib.FTP.ntransfercmd() now closes the socket if an error occurs Initial patch by Victor Stinner.
Diffstat (limited to 'Lib/ftplib.py')
-rw-r--r--Lib/ftplib.py56
1 files changed, 31 insertions, 25 deletions
diff --git a/Lib/ftplib.py b/Lib/ftplib.py
index 8713e47..c896433 100644
--- a/Lib/ftplib.py
+++ b/Lib/ftplib.py
@@ -325,33 +325,39 @@ class FTP:
if self.passiveserver:
host, port = self.makepasv()
conn = socket.create_connection((host, port), self.timeout)
- if rest is not None:
- self.sendcmd("REST %s" % rest)
- resp = self.sendcmd(cmd)
- # Some servers apparently send a 200 reply to
- # a LIST or STOR command, before the 150 reply
- # (and way before the 226 reply). This seems to
- # be in violation of the protocol (which only allows
- # 1xx or error messages for LIST), so we just discard
- # this response.
- if resp[0] == '2':
- resp = self.getresp()
- if resp[0] != '1':
- raise error_reply, resp
+ try:
+ if rest is not None:
+ self.sendcmd("REST %s" % rest)
+ resp = self.sendcmd(cmd)
+ # Some servers apparently send a 200 reply to
+ # a LIST or STOR command, before the 150 reply
+ # (and way before the 226 reply). This seems to
+ # be in violation of the protocol (which only allows
+ # 1xx or error messages for LIST), so we just discard
+ # this response.
+ if resp[0] == '2':
+ resp = self.getresp()
+ if resp[0] != '1':
+ raise error_reply, resp
+ except:
+ conn.close()
+ raise
else:
sock = self.makeport()
- if rest is not None:
- self.sendcmd("REST %s" % rest)
- resp = self.sendcmd(cmd)
- # See above.
- if resp[0] == '2':
- resp = self.getresp()
- if resp[0] != '1':
- raise error_reply, resp
- conn, sockaddr = sock.accept()
- if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT:
- conn.settimeout(self.timeout)
- sock.close()
+ try:
+ if rest is not None:
+ self.sendcmd("REST %s" % rest)
+ resp = self.sendcmd(cmd)
+ # See above.
+ if resp[0] == '2':
+ resp = self.getresp()
+ if resp[0] != '1':
+ raise error_reply, resp
+ conn, sockaddr = sock.accept()
+ if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT:
+ conn.settimeout(self.timeout)
+ finally:
+ sock.close()
if resp[:3] == '150':
# this is conditional in case we received a 125
size = parse150(resp)