本文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
参考: