pysftp 给出 pysftp.exceptions.ConnectionException: (host, port) 没有详细信息

pysftp gives pysftp.exceptions.ConnectionException: (host, port) with no details on it

我正在尝试使用 pysftp 库连接到 sftp 服务器。这是我的代码:

import pysftp

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

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

它给了我例外:

pysftp.exceptions.ConnectionException: ('host', 端口)

但是我不知道这个异常是什么意思,问题是什么。

你没有太多的解释,因为这个库有错误。请参阅 BitBucket.

上的源代码

ConnectionException class 没有很好地实现:

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'

如您所见,格式'Could not connect to host:port. %s:%s'没有填写hostport 个值。

不过,异常的名称很明确:你有一个连接错误。

不幸的是,错误的详细信息丢失了:

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)

你可以尝试获取最后一个错误(不确定):

import sys

sys.exc_info()

注意:我建议你使用另一个库(例如Paramiko)。