修改了幾個部分:python
遞歸複製的代碼參考了一些網上的代碼,其中json
try: sftp.put(real_local_path, real_server_path) except: sftp.mkdir(os.path.join(server_path, dirpath).replace("\\", "/")) sftp.put(real_local_path, real_server_path)
這段在不少示例中是先採用 mkdir 建立目錄,捕捉到異常後pass,而後再複製文件。具體考慮到個人實際狀況,是文件夾已經存在的狀況比較多,因此採起首先嚐試同步文件的策略。小程序
因爲是單線程,一條鏈接,因此在須要同步的文件多的狀況下,效率仍是個很大的問題。服務器
修改後的源碼:函數
#!-*- coding: utf-8 -*- """ 練手小程序 """ import json import paramiko import os # 遞歸複製文件 def put_recursive(sftp, local_path, server_path): sync_file_count = 0 local_path_split = os.path.split(local_path) os.chdir(local_path_split[0]) local_path_parent = local_path_split[1] for (dirpath, dirnames, files) in os.walk(local_path_parent): for file in files: real_local_path = os.path.join(dirpath, file) real_server_path = os.path.join(server_path, dirpath, file).replace("\\", "/") try: sftp.put(real_local_path, real_server_path) except: sftp.mkdir(os.path.join(server_path, dirpath).replace("\\", "/")) sftp.put(real_local_path, real_server_path) print("已同步本地文件: '{}' 到服務器路徑:'{}'".format(local_path_split[0] + real_local_path, real_server_path)) sync_file_count += 1 return sync_file_count def main(): default_port = 22 global_sync_file_count = 0 with open("config.json") as config_file: config = json.load(config_file) print("讀取配置文件成功!") host_config = config["host_config"] for host in host_config: try: ip = host["ip"] port = host.get("port", default_port) username = host["username"] password = host["password"] path_list = host["path_list"] sync_file_count = 0 print("開始鏈接遠程服務器:{}".format(ip)) transport = paramiko.Transport((ip, port)) transport.connect(username=username, password=password) sftp = paramiko.SFTPClient.from_transport(transport) print("鏈接遠程服務器 '{}' 成功!".format(ip)) print("********同步文件到服務器 '{}' --開始".format(ip)) for path in path_list: sync_file_count = put_recursive(sftp, path["local_path"], path["server_path"]) global_sync_file_count += sync_file_count print("********同步文件到服務器 '{}' --結束,同步了 {} 個文件".format(ip, sync_file_count)) except IOError: pass finally: sftp.close() transport.close() print("成功關閉與遠程服務器'{}'的鏈接".format(ip)) print("已經所有同步完成!一共同步了 {} 個文件!".format(global_sync_file_count)) if __name__ == '__main__': import time main() print("共用了", time.clock(), "秒")
配置文件:線程
{ "host_config":[ { "ip":"127.0.0.1", "port":666, "username":"root", "password":"root", "path_list":[ { "local_path":"G:/Java", "server_path":"/home" } ] }, { "ip":"127.0.0.1", "username":"root", "password":"root", "path_list":[ { "local_path":"G:/uploadFile", "server_path":"/home" } ] } ] }