"getaddrinfo failed" 使用 Paramiko 连接到 SFTP 服务器时

"getaddrinfo failed" when connecting to SFTP server using Paramiko

我正在尝试连接到 SFTP 服务器,但是 returns 错误:

[Errno 11001] getaddrinfo failed

我正在使用 Python 3.7.3,Paramiko 版本是 2.6.0

import paramiko

host_name = "sftp://81.149.151.143"
user_name = "******"
password = "******"

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host_name, port=220, username=user_name, password=password)

ftp_client=ssh_client.open_sftp()
ftp_client.put('***/issue_1.docx', '/issue_1.docx') 
ftp_client.close()

这是完整的错误:

Traceback (most recent call last):
  File "/sftp/paramiko_bot.py", line 10, in <module>
    ssh_client.connect(hostname=host_name, port=22, username=user_name, password=password)
  File "\Local\Programs\Python\Python37\lib\site-packages\paramiko\client.py", line 340, in connect
    to_try = list(self._families_and_addresses(hostname, port))
  File "\Local\Programs\Python\Python37\lib\site-packages\paramiko\client.py", line 204, in _families_and_addresses
    hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM
  File "\AppData\Local\Programs\Python\Python37\lib\socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

SSHClient.connect should contain a hostnamehostname 参数(或者在您的情况下是 IP 地址)——不是任何类型的 URL。

ssh_client.connect(hostname="81.149.151.143", port=220, username=..., password=...)

强制性警告:请勿以这种方式使用 AutoAddPolicy – 您正在失去针对 MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".

的保护