Paramiko 中 sftp.get 超时
Timeout for sftp.get in Paramiko
我发现这很有用但是这里的回调函数也考虑了建立连接的时间。我只需要限制 SFTP get
文件传输时间。我尝试如下修改回调函数,但它不起作用。这是我的代码。
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()
sftp.get(remote_path, local_path, _timer(start_time))
你在这里没有传递 _timer
作为回调,你正在调用 _timer
函数并传递它的 return 值(它有 none)。
这是正确的(与原始代码一样):
sftp.get(remote_path, local_path, _timer)
我发现这很有用get
文件传输时间。我尝试如下修改回调函数,但它不起作用。这是我的代码。
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()
sftp.get(remote_path, local_path, _timer(start_time))
你在这里没有传递 _timer
作为回调,你正在调用 _timer
函数并传递它的 return 值(它有 none)。
这是正确的(与原始代码一样):
sftp.get(remote_path, local_path, _timer)