Simple FTP Client

Andrew Bennetts andrew-pythonlist at puzzling.org
Sun Feb 2 02:27:11 CET 2003


On Sat, Feb 01, 2003 at 08:09:25PM -0500, Metnetsky wrote:
> I'm trying to write a very small script that will connect to a host and 
> download a file (much more to come in the future).  The following is what 
> I've assembled from the ftplib manual.  
> 
>         from ftplib import FTP
> 
>         ftp = FTP('ftp.thepaperclipse.com')
>         ftp.login("drgoslee", "patribus")
>         ftp.retrbinary('RETR ./httpdocs/dbbackups/PaperClipse_sql.tar.gz');
>         ftp.quit()
> 
> Naturally however, I am getting an error.
> 
>         ftp.retrbinary('RETR/usr/local/psa/home/vhosts/thepaperclipse.com/\
>         httpdocs/dbbackups/PaperClipse_sql.tar.gz');
>         
>         TypeError: retrbinary() takes at least 3 arguments (2 given)
> 
> It makes sense, but I don't know how to correct it.  What do I need to set 
> the other parameter(s) to?  Any suggestions would be greatly appreciated.

As stated at http://www.python.org/doc/current/lib/ftp-objects.html:
    
    retrbinary(command, callback[, maxblocksize[, rest]])
        Retrieve a file in binary transfer mode. command should be an
        appropriate "RETR" command: 'RETR filename'. The callback function
        is called for each block of data received, with a single string
        argument giving the data block. The optional maxblocksize argument
        specifies the maximum chunk size to read on the low-level socket
        object created to do the actual transfer (which will also be the
        largest size of the data blocks passed to callback). A reasonable
        default is chosen. rest means the same thing as in the transfercmd()
        method.

You need to pass a callback method, which will be called with each piece of
data as it comes in.

E.g.

    f = open('downloaded.file', 'w')
    ftp.retrbinary(....,  f.write)

-Andrew.






More information about the Python-list mailing list