【问题标题】:pysftp gives pysftp.exceptions.ConnectionException: (host, port) with no details on itpysftp 给出 pysftp.exceptions.ConnectionException: (host, port) 没有详细信息
【发布时间】:2017-09-15 21:48:58
【问题描述】:

我正在尝试使用 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', port)

但我不知道这个异常意味着什么以及问题是什么。

【问题讨论】:

    标签: python-2.7 sftp pysftp


    【解决方案1】:

    你没有太多解释,因为这个库有错误。查看BitBucket上的源代码。

    ConnectionException 类没有很好的实现:

    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'
    

    如您所见,格式'无法连接到主机:端口。 %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)。

    【讨论】:

    • 是的,我不得不切换到 paramiko。 Ty 为您的回应
    猜你喜欢
    • 2018-05-30
    • 2019-07-31
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多