[Tutor] ftplib and zipfile question

VanL vlindberg@verio.net
Mon, 26 Mar 2001 17:13:41 -0700


Hello,
I am writing a program to download something from an ftp site.  I have
checked the ftplib documentation and I know that I need the retrbinary
command.  However, the docs say:

retrbinary (command, callback[, maxblocksize[, rest]]) 
Retrieve a file in binary transfer mode. command should be an
appropriate "RETR" command, i.e. 'RETR filename'. The callback function
is called for each block of data received, with a single string argument
giving the data block ....

What is the callback command?  Could anyone give an example of the use
of this command to download a file and save it locally?

Further, does python have anything like the Unix pipe?  The file in
question is a zip file.  If possible, I would rather just pipe it
through to the zipfile module.

In short, is this possible?

++++++++++++++++++++++++++++++++++++++

import zlib, zipfile
from ftplib import FTP

ftp = FTP('mysite.com', 'anonymous', 'me@myaddress.com')
ftp.cwd('newdir')

myzip = Zipfile((ftp.retrbinary('binaryfile.zip', OTHER_ARGS_HERE), 'r')
for filename in myzip.namelist:
	file = open(filename, 'w')
	file.write(myzip.read(filename))

ftp.close()

+++++++++++++++++++++++++++++++++++++++++

If possible, I would like to process the file as it goes through,
without either incurring huge memory usage or having to save the file to
disk.

If I have to choose one or the other, tho, I would store the file to
disk.