2

I try to upload a file to a server via sftp using paramiko.

def send_file(server_, port_, user_, passwd_, file_, dir_):
    """
    :return:
    """
    try:
        transport = paramiko.Transport((server_, int(port_)))
        transport.connect(username=user_, password=passwd_)
        sftp = paramiko.SFTPClient.from_transport(transport)
        sftp.put(file_, dir_)
        sftp.close()
    except RuntimeError, err:
        print(str(err))

If I execute this function it just hangs (no response, no error messages), until the socket times out.

The credentials are correct, I tried them with sftp and ssh clients from the same machine and the same network. I also passed the Transport and connect values directly, no change. The logs on the server_ don't show any connections when I use this function. The host key is in my known_hosts file.

The first statement in the try-block succeeds (I passed a string instead of an int to port_, this throws an exception), the second line seems to have problems.

What's the problem here?

Thanks in advance!

UPDATE 1:

I tried this in ipython2 and it works. The function above is in a PyQt program and executed via

self.connect(self.b_upload, QtCore.SIGNAL("clicked()"), self.onUpload)

Function onUpload:

def onUpload(self):
    file_, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Datei inklusive Pfad angeben: ')
    server_, port_, user_, passwd_, dir_ = ftpmod.read_config()
    ftpmod.send_file(server_, port_, user_, passwd_, file_, dir_)
Steffen
  • 733
  • 2
  • 10
  • 24
  • Last Update: 2014-08-25. So: Nope. – Steffen Sep 12 '14 at 08:15
  • It seems this is a bug(?) in paramiko, see [this][1] and [this][2] question. [1]: http://stackoverflow.com/questions/443387/why-does-paramiko-hang-if-you-use-it-while-loading-a-module/450895#450895 [2]: http://stackoverflow.com/questions/13155890/paramiko-hanging-during-authentication-when-runned-by-dint-of-unittest-runner?lq=1 – Steffen Sep 12 '14 at 09:28
  • Please read link [2], it was still there in '12 and the problem is exact the same, a lock in the connect() thread. So, again: Nope. – Steffen Sep 12 '14 at 09:48

1 Answers1

0

You can send via sftp, using ssh.open_sftp:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
    paramiko.AutoAddPolicy())
ssh.connect(host, username, password)
ftp = ssh.open_sftp()
ftp.put(localpath, remotepath)
ftp.close()
German Petrov
  • 1,475
  • 1
  • 17
  • 21