hivefans
7/28/2015 - 10:22 AM

ftp下载显示现在进度

ftp下载显示现在进度

import ftplib
import sys
from progressbar import ProgressBar
from progressbar.widgets import Percentage, FileTransferSpeed, ETA, Bar


if len(sys.argv) < 4:
    print("请输入ftp地址,用户,密码和文件".decode("utf8"))
    sys.exit(0)

ftp = ftplib.FTP(sys.argv[1], sys.argv[2], sys.argv[3])


files = sys.argv[4].split(',')

for f in files:
    filesize = ftp.size("/" + f)

    widgets = ['Download [' + f + ']: ', Percentage(), ' ',
               Bar(marker='*', left='[', right=']'),
               ' ', ETA(), ' ', FileTransferSpeed()]

    p = ProgressBar(maxval=filesize, widgets=widgets).start()

    with open(f, 'w') as f2:
        def callback(chunk):
            f2.write(chunk)
            p.update(p.currval + len(chunk))
        ftp.retrbinary('RETR /' + f, callback)

    p.finish()