0

I'm trying to connect to sftp server with pysftp library. Here is my code:

import pysftp

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

with pysftp.Connection("sftp://host", "login", "password", cnopts=cnopts) as sftp:
    sftp.listdir()

It gives me exception:

pysftp.exceptions.ConnectionException: ('host', port)

But I have no clue what this exception means and what the problem is.

wasd
  • 1,532
  • 3
  • 28
  • 40

1 Answers1

1

You don't have much explanation because this library has bugs. See the source code on BitBucket.

The ConnectionException class is not well implemented:

class ConnectionException(Exception):
    """Exception raised for connection problems

    Attributes:
        message  -- explanation of the error
    """

    def __init__(self, host, port):
        # Call the base class constructor with the parameters it needs
        Exception.__init__(self, host, port)
        self.message = 'Could not connect to host:port.  %s:%s'

As you can see, the format 'Could not connect to host:port. %s:%s' is not filled with the host and port values.

However, the name of the exception is clear: you have a connection error.

The details of the error are, unfortunately, lost:

def _start_transport(self, host, port):
    '''start the transport and set the ciphers if specified.'''
    try:
        self._transport = paramiko.Transport((host, port))
        # Set security ciphers if set
        if self._cnopts.ciphers is not None:
            ciphers = self._cnopts.ciphers
            self._transport.get_security_options().ciphers = ciphers
    except (AttributeError, socket.gaierror):
        # couldn't connect
        raise ConnectionException(host, port)

You can try to get the last error (not sure):

import sys

sys.exc_info()

note: I suggest you to use another library (for instance Paramiko).

Community
  • 1
  • 1
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103