1

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()
Mahesh Karia
  • 2,045
  • 1
  • 12
  • 23
nilesh
  • 13
  • 4
  • Hi @MartinPrikryl , I've recently started working in python. In the above code the callback function also takes into account connection establishment time. I need to restrict only the file transfer time. I tried to modify the callback function but it doesn't work if I use the start time to be the time just before the sftp get call. – nilesh Dec 19 '17 at 11:08

1 Answers1

0
sftp.get(remote_path, local_path, _timer(start_time))

You are not passing _timer as callback here, you are calling _timer function and passing its return value (it has none).

This is correct (as the original code does):

sftp.get(remote_path, local_path, _timer)
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992