Martin Franklin wrote:
Robert wrote:
I just tried to convert a (hugh size) ftp.retrbinary run into a
pseudo-file object with .read(bytes) method in order to not consume
500MB on a copy operation.
[snip]
Hmmmm this is nearly there I think...:
whoops... spoke too soon..
import ftplib
class TransferAbort(Exception): pass
class FTPFile:
def __init__(self, server, filename):
self.server = server
self.filename = filename
self.offset = 0
def callback(self, data):
self.offset = self.offset + len(data)
self.data = data
## now quit the RETR command?
raise TransferAbort("stop right now")
def read(self, amount):
self.ftp = ftplib.FTP(self.server)
self.ftp.login()
I needed to insert a time.sleep(0.1) here as the connections were
falling over themselves - I guess testing with a blocksize of 24
is a little silly.
try:
self.ftp.retrbinary("RETR %s" %self.filename, self.callback,
blocksize=amount,
rest=self.offset)
except TransferAbort:
return self.data
f = FTPFile("HOSTNAME", "FILENAME")
print f.read(24)
print f.read(24)
## new test...
f = FTPFile("HOSTNAME", "FILENAME")
while 1:
data = f.read(24)
if not data:
break
print data,
I open the ftp connection inside the read method as it caused an error
(on the second call to read) when I opened it in __init__ ???
HTH
Martin
--
http://mail.python.org/mailman/listinfo/python-list