I found this to be useful Setting timelimit for sftp.get() of Paramiko module but the callback function here also takes into account time for connection establishment. I need to restrict only the SFTP get
file transfer time. I tried to modify the callback function as below but it doesn't work. Here's my code.
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()