1

I'm trying to use the paramiko module to copy a (big) file in my local network, and get the output to display a GtkProgressBar. A part of my code is:

    ...
        NetworkCopy.pbar.set_text("Copy of the file in the Pi...")
        while gtk.events_pending():            # refresh the progress bar
              gtk.main_iteration()   

        self.connection(transferred, toBeTransferred)

  def connection(self, transferred, toBeTransferred):

        sftp = self.sftp
        fichier_pc = self.fichier_pc
        chemin_pi = self.chemin_pi             # var names are in french !
        fichier = self.fichier
        transferred = self.transferred
        toBeTransferred = self.toBeTransferred
        print "Transferred: {0}\tStill to send: {1}".format(transferred, toBeTransferred)
        sftp.put(fichier_pc, chemin_pi + fichier, callback=self.connection)

In the terminal, I can see

Transferred: 0  Still to send: 3762398252

for a while, but after 10s I have this error:

  File "network_copier.py", line 158, in connection
    sftp.put(fichier_pc, chemin_pi + fichier, callback=self.connection)
  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 615, in put
    return self.putfo(fl, remotepath, os.stat(localpath).st_size, callback, confirm)
  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 577, in putfo
    fr.close()
  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_file.py", line 67, in close
    self._close(async=False)
  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_file.py", line 88, in _close
    self.sftp._request(CMD_CLOSE, self.handle)
  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 689, in _request
    return self._read_response(num)
  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 721, in _read_response
    raise SSHException('Server connection dropped: %s' % (str(e),))
paramiko.SSHException: Server connection dropped: 

I have the 1.12.2 version of paramiko, from this ppa

Thanks for your help

Edit: The solution is to use pexpect instead of paramiko. It's working with big files. See here

Community
  • 1
  • 1
Guillaume
  • 2,752
  • 5
  • 27
  • 42
  • try [this code](http://stackoverflow.com/a/20381739/4279); replace `sftp.get` with `sftp.put` to upload instead of downloading. – jfs Mar 28 '14 at 09:46
  • With sftp.put the terminal says that: IOError: [Errno 2] No such file – Guillaume Mar 28 '14 at 10:01
  • you also need to replace `sftp.listdir()` with `os.listdir('.')` because you are copying local files to remote directory therefore you need to list local paths – jfs Mar 28 '14 at 10:06
  • 1
    Huhh... paramiko bug with big files... see [here](https://github.com/paramiko/paramiko/issues/151) Damage ! – Guillaume Mar 28 '14 at 10:52
  • does [this fabric-based solution also fail](http://stackoverflow.com/a/22710513/4279)? (it is based on `paramiko` so it should fail) – jfs Mar 28 '14 at 10:59
  • I've failed to use "def progress(locals):" -> "NetworkCopy instance has no attribute '__getitem__' " (sorry I'm beginner) But yes, it should fail... I will try the first way you have wrote – Guillaume Mar 28 '14 at 11:22
  • 1. `def progress()` is defined for [`pexpect`-based solution](http://stackoverflow.com/a/22695208/4279). And it works as is (I've checked). 2. "should fail" doesn't mean that it fails. Check [`fabric`-based solution](http://stackoverflow.com/a/22710513/4279) to be sure. – jfs Mar 28 '14 at 11:48
  • btw, I've looked at scp protocol description (unrelated to sftp). [There is effectively no size limit](https://blogs.oracle.com/janp/entry/how_the_scp_protocol_works) i.e., `scp` should work – jfs Mar 28 '14 at 11:57
  • It's working well ! I didn't know that a def could be in another def. Thanks a lot – Guillaume Mar 28 '14 at 14:09
  • yes. You may have nested function definitions. Though there is none in my code related to `scp`. Which solution works: pexpect or fabric? btw, [you could add your own answer (about the size-related paramiko bug) and accept it. It is even encouraged](http://stackoverflow.com/help/self-answer) and mention the working solution as a workaround – jfs Mar 28 '14 at 14:19
  • I've used pexpect successfully. But I can't answer my own question since I've not 15 of reputation, just a comment. – Guillaume Mar 28 '14 at 14:28
  • @Sebastian: Hi, I have a new problem with spaces in the file name. My question is [here](http://stackoverflow.com/questions/22729431/python-scp-copy-file-with-spaces-in-filename/22729561?noredirect=1#comment34641353_22729561), maybe you have a solution for me. Thanks per advance. – Guillaume Mar 29 '14 at 13:02

0 Answers0