Python远程下载文件进度条

由于工作需要,需要将服务器上的日志下载到本地。由于文件比较大,想查看下载的进度。

def progress_bar(transferred, toBeTransferred, suffix=''):
    bar_len = 100
    filled_len = int(round(bar_len * transferred / float(toBeTransferred)))
    percents = round(100.0 * transferred / float(toBeTransferred), 1)
    bar = '\033[32;1m%s\033[0m' % '=' * filled_len + '-' * (bar_len - filled_len)
    sys.stdout.write('[%s] %s%s %s\r' % (bar, '\033[32;1m%s\033[0m' % percents, '%', suffix))
    sys.stdout.flush()

def downfile():
    """
    下载文件
    :param hostip:
    :param parajson:
    :return:
    """
    hostip=‘192.168.1.5’
    sshport = '22'
    password = 'sanshi@408'
    oldfilepath = '/root/oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm'
    newfilepath = '/root/oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm'

    t = paramiko.Transport((hostip, int(sshport)))
    t.connect(username='root', password=password)
    sftp = paramiko.SFTPClient.from_transport(t)
    sftp.get(oldfilepath, newfilepath, callback=progress_bar)
    t.close()
    return ''

效果:
在这里插入图片描述

你可能感兴趣的:(Python)