ftplib and FTP Objects...

Steve Holden sholden at holdenweb.com
Mon Apr 22 15:28:55 EDT 2002


"Audun Tonnesen" <aut at c2i.net> wrote in message
news:1103_1019500994 at news1.c2i.net...
> I have problems with retrbinary. The call is like this:
> ftpobject.retrbinary("RETR <filename>, file.write(someData),[blocksize])
>
> My problem is the <someData> argument to file.write(...). How do I define
or bind the name to the actual datachunk?
>

Your code should read something like what follows:

import ftplib
myfile = open("theoutputfile.bin", "wb")

def writer(x):
    """This function is called with  x containing data each time
    a block of data becomes available from the ftplib.FTP object."""
    myfile.write(x)

ftpobject = ftplib.FTP( ... whatever ...)

ftpobject.retrbinary("RETR myfile.bin", writer)

myfile.close()
ftpobject.quit()

In other words, you pass a *function* to retrbinary(), and retrbinary()
calls the function with a single argument each time it has data to be
handled. That's why the documentation refers to it as a "callback".

See the book URL below for more reading on this ;-)

regards
 Steve
--

home: http://www.holdenweb.com/    book: http://pydish.holdenweb.com/pwp/







More information about the Python-list mailing list