Re: [Tutor] FTP Binary Mode.

Magnus Lycka magnus at thinkware.se
Thu Apr 29 05:36:46 EDT 2004


Chad Crabtree wrote:
> I am modifying the thumbnail.py file from useless
> python to connect via ftp and make thumbnails.  I'm
> the snag I've ran in to is that 
> ftp.FTP.retrbinar(cmd,callback,blockisize)
> on does things through the callback function.  I would
> like to create a file object with it somehow but I'm
> stumped.  I know I could make it into a big string but
> then I would need to turn it into a file object.  I'm
> really stumped.

Open a file (f) for writing (binary).
Supply f.write as callback function for retrbinary.
That's basically it!

>>> import ftplib
>>> import os.path
>>> host = 'ftp.sunet.se'
>>> path = '/pub/comics/comics/Snoopy/snoopy.gif'
>>> local_dir = 'H:\\'
>>> ftp = ftplib.FTP(host)
>>> ftp.login()
'230 Login successful. Have fun.'
>>> dir, fn = os.path.split(path)
>>> local_path = os.path.join(local_dir, fn)
>>> ftp.cwd(dir)
'250 Directory successfully changed.'
>>> f = file(local_path, 'wb')
>>> ftp.retrbinary('RETR '+fn, f.write)
'226 File send OK.'
>>> f.close()
>>> ftp.close()

It worked! :)

-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list