Chris Curvey
Chris Curvey

Reputation: 10389

paramiko can't open SFTP connection

I'm having some trouble opening an SFTP connection with paramiko. My current code is:

client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())
client.load_system_host_keys()
client.connect('some.example.com', username="myuser", password="mypassword")
sftp_client = client.open_sftp()
sftp_client.put(my_local_file)

But at the point where I hit client.open_sftp(), I get an exception of "Unable to open channel."

Any idea what might cause this? I've been able to open the connection to the server with a command-line sftp client.

I'm guessing about my invocation here, if anyone can point me to an example, that would be great.

Upvotes: 2

Views: 7060

Answers (1)

agf
agf

Reputation: 176780

You need to first create and connect to a transport:

transport = Transport((host, port))
transport.connect(username = username, pkey = mykey) # or password = mypassword

Now to can start the SFTP client:

sftp_client = SFTPClient.from_transport(transport)

Then you can

sftp_client.put(my_local_file)

and when you're done

sftp_client.close()
transport.close()

Upvotes: 6

Related Questions