1

I am trying to connect to a server using the following script:

import pysftp

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None 

srv = pysftp.Connection(host="sftp://my_ip:my_port", 
                        username="alessandro", password="alessandro", port=2222,
                        cnopts=cnopts)
data = srv.listdir()
srv.close()

for i in data:
    print (i)

If I try to access via FileZilla or similar clients, with the same credentials, it works; even though it displays the following warning, concerning host key algorithm and fingerprint as:

enter image description here

however, the script does not run properly and reports the following error:

SSHException: Unable to connect to sftp://my_ip:my_port: [Errno 60] Operation timed out

Even if I try with host = my_ip only. Additionally, if I do not set cnopts, it does not find the host key.

How can I solve it?

Alessandro Ceccarelli
  • 1,527
  • 2
  • 14
  • 33

1 Answers1

1

The host parameter of Connection constructor is:

The Hostname or IP of the remote machine.

Not an URL.

So it should be:

srv = pysftp.Connection(host="my_ip", 
                        username="alessandro", password="alessandro", port=2222,
                        cnopts=cnopts)

Additionally, do not set the cnopts.hostkeys = None, unless you do not care about security.
For the correct solution, see Verify host key with pysftp
.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846