我发现这有用Setting timelimit for sftp.get() of Paramiko module但这里的回调函数也考虑了连接建立的时间。我只需要限制SFTP class TimeLimitExceeded(Exception):
pass
def _timer(start_time, timelimit=5):
elapsed_time = time.time()-start_time
if elapsed_time > timelimit:
raise TimeLimitExceeded
if __name__=="__main__":
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, username, password)
sftp = ssh.open_sftp()
try:
start_time=time.time()
sftp.get(remote_path, local_path, _timer(start_time))
except TimeLimitExceeded:
print ("The operation took too much time to complete")
finally:
sftp.close()
ssh.close()
文件传输时间。我尝试修改回调函数,如下所示,但它不起作用。这是我的代码。
model.update({'FirstName':'Mohammad'}, {$push:{PhoneNumbers: NumberInt(1234567890)}}, function (err, updatedModelObj) {
if (err) {
console.log(err);
}
callback(updatedModelObj);
})
答案 0 :(得分:0)
sftp.get(remote_path, local_path, _timer(start_time))
您没有在此处传递_timer
作为回调,您正在调用_timer
函数并传递其返回值(它没有)。
这是正确的(正如原始代码所做的那样):
sftp.get(remote_path, local_path, _timer)