SSHTunnelForwarder - "IP is not a string (NoneType)"

SSHTunnelForwarder - "IP is not a string (NoneType)"

我正在尝试使用 SSHTunnelForwarder。我收到一个错误 "IP is not a string (NoneType)"。如果我理解正确的话,这意味着我传递的宿主变量是 NoneType 类型并且需要是 String 类型。打印出变量类型表明它是字符串类型:.

我已经概括了相关代码。见下文:

def connectToClient(client):
    key, host = getClientInfo(client)
    # Both of the below print statements return <class 'str'>
    print(type(key))
    print(type(host))

    try:
        with SSHTunnelForwarder(
            ssh_username = "username",
            ssh_private_key = key,
            remote_bind_address = (host, 22)) as server:
                # Do stuff here

    except Exception as e:
        # Handle exception

堆栈跟踪(编辑以隐藏文件系统信息和 .py 文件名。同时使用 0.0.0.0 进行测试):

Traceback (most recent call last):
  File "/path/to/file/myPythonFile.py", line 83, in connectToClient
    remote_bind_address = ('0.0.0.0', 22)) as server:
  File "/path/to/file/lib/python3.6/site-packages/sshtunnel.py", line 908, in __init__
    check_host(self.ssh_host)
  File "/path/to/file/lib/python3.6/site-packages/sshtunnel.py", line 79, in check_host
    type(host).__name__
AssertionError: IP is not a string (NoneType)

我想指出的是,即使我将主机变量替换为包含 IP 的字符串(即“127.0.0.1”),我仍然收到相同的错误。

也许我犯了一个简单的错误。有什么想法吗?

编辑:修复格式并添加堆栈跟踪

您缺少一个参数。见 example.
实际上,您在调用 SSHTunnelForwarder 时没有关键字参数 ssh_address_or_host,它表示远程主机的 IP 和可选的端口号。
确实,您混淆了 remote_bind_address 使用它的含义,因为它是 ssh_address_or_host.

当该方法检查与 IP 相关的主机部分时,将找不到字符串,但 None 出现异常。见 code.