Python paramiko文件传输显示上传下载进度信息 - print

本文Python Code基于chatGPT的推荐,并修改调试实际运行通过,供学习参考。

通过使用python paramiko库,实现文件传输,包括上传和下载,并且同步显示上传和下载进度,通过print的方式,paramiko实际使用的是SSH协议,所以默认传输分片大小为32768bytes,所以print打印的次数,为文件的大小除以分片大小。

Python代码

import os
import paramiko
import time

def download_file_with_progress(hostname, username, password, remote_path, local_path):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=hostname, username=username, password=password)
    sftp = ssh.open_sftp()
    # get remote file size
    remote_file_size = sftp.stat(remote_path).st_size
    print ("remote_file_size:{}".format(remote_file_size))
    # print progress
    with open(local_path, 'wb') as f:
        def callback(transferred, remote_file_size):
            percent = float(transferred) * 100 / remote_file_size
            print("Download %.2f%% of the file." % percent)
        # transfer 32768 bytes as SSH slice, get remote file to local
        sftp.getfo(remote_path, f, callback=callback)
    sftp.close()
    ssh.close()

def upload_file_with_progress(hostname, username, password, remote_path, local_path):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=hostname, username=username, password=password)
    sftp = ssh.open_sftp()
    # get local file size
    local_file_size = os.stat(local_path).st_size
    print ("local_file_size:{}".format(local_file_size))
    # print progress
    with open(local_path, 'rb') as f:
        def callback(transferred, local_file_size):
            percent = float(transferred) * 100 / local_file_size
            print("Upload %.2f%% of the file." % percent)
        # transfer 32768 bytes as SSH slice, put local file to remote
        sftp.putfo(f, remote_path, local_file_size, callback=callback)
    sftp.close()
    ssh.close()

if __name__ == "__main__":
    hostname = "host_IP"
    username = "host_username"
    password = "host_password"
    remote_path = "~/file"
    local_path = "./file.download"
    remote_path_upload = "~/file.upload"
    local_path_upload = "./file"

    # download file and calculate cost
    print ("Download Start, 32678 bytes as default SSH slice: ")
    download_start = time.time()
    download_file_with_progress(hostname, username, password, remote_path, local_path)
    download_end = time.time()
    download_cost = download_end - download_start
    print ("Download successfully! Cost {}s".format(download_cost))

    cutline = '\033[32;1m%s\033[0m' % '=' * 100
    print (cutline)
    # upload file and calculate cost
    print ("Upload Start, 32678 bytes as default SSH slice: ")
    upload_start = time.time()
    upload_file_with_progress(hostname, username, password, remote_path_upload, local_path_upload)
    upload_end = time.time()
    upload_cost = upload_end - upload_start
    print ("Upload successfully! Cost {}s".format(upload_cost))

输出

$ python download_upload_big_file_progress_print.py
Download Start, 32678 bytes as default SSH slice:
remote_file_size:293076
Download 11.18% of the file.
Download 22.36% of the file.
Download 33.54% of the file.
Download 44.72% of the file.
Download 55.90% of the file.
Download 67.08% of the file.
Download 78.27% of the file.
Download 89.45% of the file.
Download 100.00% of the file.
Download successfully! Cost 0.20436573028564453s
====================================================================================================
Upload Start, 32678 bytes as default SSH slice:
local_file_size:293076
Upload 11.18% of the file.
Upload 22.36% of the file.
Upload 33.54% of the file.
Upload 44.72% of the file.
Upload 55.90% of the file.
Upload 67.08% of the file.
Upload 78.27% of the file.
Upload 89.45% of the file.
Upload 100.00% of the file.
Upload successfully! Cost 0.1543407440185547s

参考:

Python实现文件的简单传输上传和下载代码_Entropy-Go的博客-CSDN博客

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值