0

My below code taking time to do sftp upload using paramiko

   diff=0
   while diff < 5:
         t1=time.time()
         ftp_client.put("./py_scripts/"+py_file,sys.argv[10]+"/"+py_file)
         t2=time.time()
         diff = t2-t1
        
        ftp_client.close()

But it took time as it is wait for ftp_client.put to complete that. How can I stop the upload in between if it take more than 5 sec

I have been tried a callback as well to get time but no hope

start_time = time.time()

def check_time(size, file_size):
    global start_time
    if (time.time() - start_time) > 20:
        raise Exception

ftp_client.put("./py_scripts/"+py_file,'/home/'+username+'/'+py_file, callback=check_time)

in pdb I found below

-> ftp_client=ssh.open_sftp()
(Pdb) s
--Call--
> c:\users\raa\appdata\local\programs\python\python39\lib\site-packages\paramiko\client.py(550)open_sftp()
-> def open_sftp(self):
(Pdb) s
> c:\users\raa\appdata\local\programs\python\python39\lib\site-packages\paramiko\client.py(556)open_sftp()
-> return self._transport.open_sftp_client()
(Pdb) s
--Call--
> c:\users\raa\appdata\local\programs\python\python39\lib\site-packages\paramiko\transport.py(1087)open_sftp_client()
-> def open_sftp_client(self):
(Pdb) n
> c:\users\raa\appdata\local\programs\python\python39\lib\site-packages\paramiko\transport.py(1097)open_sftp_client()
-> return SFTPClient.from_transport(self)
(Pdb) n

It is taking time after that

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
  • See [How to timeout Paramiko sftp.put() with signal module or other in Python?](https://stackoverflow.com/q/58844902/850848) – Martin Prikryl Dec 28 '21 at 07:18
  • Martin thanks for the update ...I have updated the question I already tried also – Rajarshi Das Dec 28 '21 at 08:45
  • You didn't show us your callback. + I've added some explanation to my answer to the above linked question to explain some issues when cancelling SFTP transfer. – Martin Prikryl Dec 28 '21 at 10:27
  • I have updated my question with the callback as well – Rajarshi Das Dec 28 '21 at 11:57
  • And what did you do to debug the problem? Is the exception raised in time? But the `put` is not cancelled anyway? Did you read the comment in my answer? Did you try using `putfo`? – Martin Prikryl Dec 28 '21 at 12:16
  • Martin no exception is occurring only it took huge amount of time like 1 hour to upload one 2kb file but normal sftp using scp is working fast – Rajarshi Das Dec 29 '21 at 03:51

0 Answers0