convert ftp.retrbinary to file object? - Python language lacks expression?
Martin Franklin
mfranklin1 at gatwick.westerngeco.slb.com
Thu Feb 3 06:37:30 EST 2005
Martin Franklin wrote:
> 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..
Trigger happy this morning...
>
>>
>> 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:
also need to close the ftp connection here!
self.ftp.close()
>> 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
>>
>>
More information about the Python-list
mailing list