summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGiampaolo Rodola' <g.rodola@gmail.com>2013-03-07 13:03:27 (GMT)
committerGiampaolo Rodola' <g.rodola@gmail.com>2013-03-07 13:03:27 (GMT)
commit3ff5e7578efbe5ba6b2ad78cd767b593284c450b (patch)
tree5d619044c5e02e9eb87d38a0bfcd046af5fd1e1a
parent94c56a172cb5f797c97af66dbf0431b0e747af6c (diff)
downloadcpython-3ff5e7578efbe5ba6b2ad78cd767b593284c450b.zip
cpython-3ff5e7578efbe5ba6b2ad78cd767b593284c450b.tar.gz
cpython-3ff5e7578efbe5ba6b2ad78cd767b593284c450b.tar.bz2
Refactor ftplib by getting rid of 4 methods (retr*() and stor*()) which were duplicated in TLS_FTP class.
-rw-r--r--Lib/ftplib.py86
1 files changed, 15 insertions, 71 deletions
diff --git a/Lib/ftplib.py b/Lib/ftplib.py
index 8abf753..9c173c4 100644
--- a/Lib/ftplib.py
+++ b/Lib/ftplib.py
@@ -439,6 +439,9 @@ class FTP:
if not data:
break
callback(data)
+ # shutdown ssl layer
+ if isinstance(conn, _SSLSocket):
+ conn.unwrap()
return self.voidresp()
def retrlines(self, cmd, callback = None):
@@ -469,6 +472,9 @@ class FTP:
elif line[-1:] == '\n':
line = line[:-1]
callback(line)
+ # shutdown ssl layer
+ if isinstance(conn, _SSLSocket):
+ conn.unwrap()
return self.voidresp()
def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
@@ -495,6 +501,9 @@ class FTP:
conn.sendall(buf)
if callback:
callback(buf)
+ # shutdown ssl layer
+ if isinstance(conn, _SSLSocket):
+ conn.unwrap()
return self.voidresp()
def storlines(self, cmd, fp, callback=None):
@@ -521,6 +530,9 @@ class FTP:
conn.sendall(buf)
if callback:
callback(buf)
+ # shutdown ssl layer
+ if isinstance(conn, _SSLSocket):
+ conn.unwrap()
return self.voidresp()
def acct(self, password):
@@ -655,8 +667,10 @@ class FTP:
try:
import ssl
except ImportError:
- pass
+ _SSLSocket = None
else:
+ _SSLSocket = ssl.SSLSocket
+
class FTP_TLS(FTP):
'''A FTP subclass which adds TLS support to FTP as described
in RFC-4217.
@@ -771,76 +785,6 @@ else:
ssl_version=self.ssl_version)
return conn, size
- def retrbinary(self, cmd, callback, blocksize=8192, rest=None):
- self.voidcmd('TYPE I')
- with self.transfercmd(cmd, rest) as conn:
- while 1:
- data = conn.recv(blocksize)
- if not data:
- break
- callback(data)
- # shutdown ssl layer
- if isinstance(conn, ssl.SSLSocket):
- conn.unwrap()
- return self.voidresp()
-
- def retrlines(self, cmd, callback = None):
- if callback is None:
- callback = print_line
- resp = self.sendcmd('TYPE A')
- conn = self.transfercmd(cmd)
- fp = conn.makefile('r', encoding=self.encoding)
- with fp, conn:
- while 1:
- line = fp.readline()
- if self.debugging > 2:
- print('*retr*', repr(line))
- if not line:
- break
- if line[-2:] == CRLF:
- line = line[:-2]
- elif line[-1:] == '\n':
- line = line[:-1]
- callback(line)
- # shutdown ssl layer
- if isinstance(conn, ssl.SSLSocket):
- conn.unwrap()
- return self.voidresp()
-
- def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
- self.voidcmd('TYPE I')
- with self.transfercmd(cmd, rest) as conn:
- while 1:
- buf = fp.read(blocksize)
- if not buf:
- break
- conn.sendall(buf)
- if callback:
- callback(buf)
- # shutdown ssl layer
- if isinstance(conn, ssl.SSLSocket):
- conn.unwrap()
- return self.voidresp()
-
- def storlines(self, cmd, fp, callback=None):
- self.voidcmd('TYPE A')
- with self.transfercmd(cmd) as conn:
- while 1:
- buf = fp.readline()
- if not buf:
- break
- if buf[-2:] != B_CRLF:
- if buf[-1] in B_CRLF:
- buf = buf[:-1]
- buf = buf + B_CRLF
- conn.sendall(buf)
- if callback:
- callback(buf)
- # shutdown ssl layer
- if isinstance(conn, ssl.SSLSocket):
- conn.unwrap()
- return self.voidresp()
-
def abort(self):
# overridden as we can't pass MSG_OOB flag to sendall()
line = b'ABOR' + B_CRLF