Sunday, July 5, 2009

Paramiko hangs.

I was looking at paramiko for use in scripts and an application I was writing where I needed to do some secure ftp. Seems like I should've recognized this, but I was caught by surprise how it would handle closing the connection when python closed. I assumed the connection would close just like an open file (so that upon exiting the python shell, it would close). However, I found that the shell hung. At first I thought it was a bug in the library, but really it's just that paramiko needs you to explicitly close the connection.


def upload_file(host, user, key, remote_filename, data):
t = paramiko.Transport(host)
try:
t.start_client()
t.auth_publickey(user, key)
t.open_session()

sftp = paramiko.SFTPClient.from_transport(t)

with tempfile.NamedTemporaryFile() as f:
f.write(data)
f.flush()
sftp.put(f.name, remote_filename)
finally:
t.close()


Another one of those situations where the answer should be obvious...

No comments:

Post a Comment