简体   繁体   中英

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM