Binary file download : ftp « Network « Python Tutorial






from ftplib import FTP
import sys

f = FTP('ftp.kernel.org')
f.login()

f.cwd('/pub/linux/kernel/v1.0')
f.voidcmd("TYPE I")

datasock, estsize = f.ntransfercmd("RETR linux-1.0.tar.gz")
transbytes = 0
fd = open('linux-1.0.tar.gz', 'wb')
while 1:
    buf = datasock.recv(2048)
    if not len(buf):
        break
    fd.write(buf)
    transbytes += len(buf)
    sys.stdout.write("Received %d " % transbytes)

    if estsize:
        sys.stdout.write("of %d bytes (%.1f%%)\r" % \
                (estsize, 100.0 * float(transbytes) / float(estsize)))
    else:
        sys.stdout.write("bytes\r")
    sys.stdout.flush()
sys.stdout.write("\n")
fd.close()
datasock.close()
f.voidresp()


f.quit()








21.24.ftp
21.24.1.Using Python to Fetch Files from an FTP Server
21.24.2.Interactive FTP Example
21.24.3.FTP Download Example
21.24.4.Binary file download
21.24.5.ASCII file download
21.24.6.Binary file upload
21.24.7.Binary file download 2
21.24.8.Basic FTP connection
21.24.9.File dir() example
21.24.10.Parse return value from dir() function
21.24.11.FTP nlst example
21.24.12.nlst() with file/directory detection example