远程下载文件并设置进度显示

 第一种方式用urlretrieve下载:

def Schedule(a, b, c):
    """
    进度条显示
    :param a:已经下载的数据块
    :param b:数据块的大小
    :param c:远程文件的大小
    :return:
    """
    per = 100.0 * a * b / c
    if per > 100:
        per = 100
    sys.stdout.write('\r')
    sys.stdout.write('\t\t%.2f%% - 已下载的大小:%d - 文件大小:%d' % (per, a * b, c))
    sys.stdout.flush()
    time.sleep(0.5)


def run():
    request.urlretrieve('https://***', 'ttt', Schedule)

 

但是urlretrieve下载大文件很慢,还总是出错:

fname = 'data/%s' % u.split('/')[-1]
if os.path.exists(fname):
    print('\t***文件已存在:', u, time.strftime('%Y-%m-%d %H:%M:%S'))
    continue
print('\n\t>>>开始下载文件:', u, time.strftime('%Y-%m-%d %H:%M:%S'))
try:
    # request.urlretrieve(u, fname, Schedule)

    response = request.urlopen(parse.quote(u, safe=':/'))
    chunk_size = 16 * 1024
    n = 0
    c = response.length
    t = c / chunk_size
    t = int(t) + 1 if int(t) < t else int(t)
    a = 0
    with open(fname, 'wb') as f:
        while True:
            n += 1
            chunk = response.read(chunk_size)
            if not chunk:
                break
            f.write(chunk)
            a += len(chunk)
            if n == 1 or n % 50 == 0 or n == t:
                per = 100.0 * a / c
                if per > 100:
                    per = 100

                sys.stdout.write('\r')
                sys.stdout.write('\t\t%.2f%% - 已下载的大小:%d - 文件大小:%d' % (per, a, c))
                sys.stdout.flush()


except Exception as e:
    print('\n\t>>>文件下载失败:', u, time.strftime('%Y-%m-%d %H:%M:%S'))
    traceback.print_exc()
    continue

参考:https://www.cnblogs.com/rkfeng/p/8366327.html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/haoxr/p/9007476.html

在Python中,您可以使用`paramiko`这个第三方库来实现SFTP传输,并下载文件。同时,您可以使用`tqdm`库来显示下载进度。以下是实现这一功能的一个基本示例: 首先,确保安装了`paramiko`和`tqdm`这两个库。可以使用pip进行安装: ```bash pip install paramiko tqdm ``` 然后,您可以使用以下代码来下载文件显示进度: ```python import paramiko from tqdm import tqdm def sftp_download_progress(sftp, remote_file_path, local_file_path): # 连接到SFTP服务器 sftp.connect(hostname='hostname', port=22, username='username', password='password') # 获取远程文件的元信息 sftp_stat = sftp.stat(remote_file_path) # 开始下载文件 with tqdm(total=sftp_stat.st_size, unit='B', unit_scale=True, desc=remote_file_path) as pbar: with open(local_file_path, 'wb') as local_file: # 打开远程文件 remote_file = sftp.open(remote_file_path, 'rb') while True: buf = remote_file.read(1024 * 1024) # 读取1MB数据 if not buf: break local_file.write(buf) pbar.update(len(buf)) remote_file.close() # 关闭SFTP连接 sftp.close() # 使用示例 # 假设您已经配置好了SFTP服务器信息 hostname = 'your_sftp_server' port = 22 username = 'your_username' password = 'your_password' sftp = paramiko.SFTPClient.from_transport(paramiko.Transport((hostname, port))) sftp.connect(hostname=hostname, port=port, username=username, password=password) sftp_download_progress(sftp, '/path/to/remote/file', '/path/to/local/file') ``` 这段代码定义了一个`sftp_download_progress`函数,该函数接收SFTP连接、远程文件路径、本地文件路径作为参数,然后使用`tqdm`来显示下载进度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值