F
F
FewSeconds2021-05-22 12:31:52
Python
FewSeconds, 2021-05-22 12:31:52

How to fix pysftp connection error?

Hello. Faced a problem connecting to the server. I’m generally in this thread for the first time, so I don’t understand why I can’t connect, it seems that I entered all the data. Google realized that you need to get the key, got the key in putigen. But absolutely nothing.

Here is the code
import pysftp
from base64 import decodebytes
import paramiko

# cnopts = pysftp.CnOpts()

# cnopts.hostkeys = None

cnopts = pysftp.CnOpts(knownhosts='ssh-rsa 2048 SHA256:PEFk7nDBYtqlmNC4VIA8qSx/ii2Gn9T5NuVKHRLM72s')

srv = pysftp.Connection(host="193.34.144.226", username="root", password="**********", cnopts = cnopts)

data = srv.listdir()

srv.close()

for i in data:
    print(i)


Here is the error

C:\Users\futgi\AppData\Local\Programs\Python\Python39\lib\site-packages\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from 193.34.144.226 ssh-rsa 2048 SHA256:PEFk7nDBYtqlmNC4VIA8qSx/ii2Gn9T5NuVKHRLM72s. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
warnings.warn(wmsg, UserWarning)
Traceback (most recent call last):
File "D:\python\FREELANCE\qa_parser\answers_parser\server_requests.py", line 11, in
srv = pysftp.Connection(host="193.34.144.226", username="root", password="*********", cnopts = cnopts)
File "C:\Users\futgi\AppData\Local\Programs\Python\Python39\lib\site-packages\pysftp\__init__.py", line 132, in __init__
self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
File "C:\Users\futgi\AppData\Local\Programs\Python\Python39\lib\site-packages\pysftp\__init__.py", line 71, in get_hostkey
raise SSHException("No hostkey for host %s found." % host)
paramiko.ssh_exception.SSHException: No hostkey for host 193.34.144.226 found.
Exception ignored in:
Traceback (most recent call last):
File "C:\Users\futgi\AppData\Local\Programs\Python\Python39\lib\site-packages\pysftp\__init__.py", line 1013, in __del__
self.close()
File "C:\Users\futgi\AppData\Local\Programs\Python\Python39\lib\site-packages\pysftp\__init__.py", line 784, in close
if self._sftp_live:
AttributeError: 'Connection' object has no attribute '_sftp_live'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2021-10-26
@scjv

Port not specified:

...
cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None  # disable host key checking.

    # Connection to external SFTP server
    with pysftp.Connection(host=SFTP_HOST,
                           port=SFTP_PORT,
                           username=SFTP_USER,
                           password=fernet_decrypt(fernet_key=SFTP_KEY, encoded=SFTP_PASS_ENC),
                           cnopts=cnopts,
                           ) as sftp:

        # Change external directory
        with sftp.cd(EXTERNAL_DIR):
            external_dir = sftp.listdir()
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question