# 如果没有安装paramiko,用pip install paramiko安装
import paramiko, os, time
def update(addr, usr, pasw, fn, target_path): trans = paramiko.Transport((addr, 22)) trans.connect(username=usr, password=pasw) sftp = paramiko.SFTPClient.from_transport(trans) sftp.put(fn, target_path) def keep_update(addr, usr, pasw, fn2target_paths, if_stop=None, call_back=None): last_modified_times = {i: None for i in fn2target_paths} while True: for i in fn2target_paths: modified_time = os.stat(i).st_mtime if (not last_modified_times[i]) or modified_time > last_modified_times[i]: last_modified_times[i] = modified_time update(addr, usr, pasw, i, fn2target_paths[i]) print(i+':', time.asctime()) if call_back: call_back() if if_stop and if_stop[0]: break time.sleep(0.2)
if __name__ == '__main__':
keep_update(ip, user_name, passwrd, {
filepath1: server_filepath1,
filepath2: server_filepath2,
})
标签:__,addr,Python,modified,实时,pasw,time,服务器,paramiko From: https://www.cnblogs.com/roundfish/p/17136160.html