1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| import paramiko from tqdm import tqdm import os
hostname = 'your_linux_server_ip' port = 22 username = 'your_linux_username' password = 'your_linux_password'
remote_path = '/home/target_path/file.txt' base_path = r'E:\source_path'
local_path = os.path.join(base_path, filename)
remote_path = '/root/' + filename
local_file_size = os.path.getsize(local_path)
ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname, port, username, password)
sftp = ssh.open_sftp()
bar_format = "{desc}: {percentage:3.0f}%|{bar:42}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}]"
with tqdm(total=local_file_size, unit='B', unit_scale=True, desc=f'\rUploading {os.path.basename(local_path)}', bar_format=bar_format, ascii="-━", ncols=42, colour='green', dynamic_ncols=True) as pbar: def callback(data_transferred, total_size): pbar.update(data_transferred - pbar.n)
sftp.put(local_path, remote_path, callback=callback) sftp.close()
ssh.close()
print(f"File {os.path.basename(local_path)} transferred successfully to {hostname}:{remote_path}")
|