“getaddrinfo 失败"使用 Paramiko 连接到 SFTP 服务器时 [英] "getaddrinfo failed" when connecting to SFTP server using Paramiko
问题描述
我正在尝试连接到 SFTP 服务器,但它返回错误:
I am trying to connect to SFTP server but it returns error:
[Errno 11001] getaddrinfo 失败
[Errno 11001] getaddrinfo failed
我使用的是 Python 3.7.3,Paramiko 版本是 2.6.0
I am using Python 3.7.3 and Paramiko version is 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
应该包含一个 主机名(或在您的情况下为 IP 地址)–不是任何类型的网址.
The hostname
parameter of SSHClient.connect
should contain a hostname only (or in your case an IP address) – not any kind of URL.
ssh_client.connect(hostname="81.149.151.143", port=220, username=..., password=...)
<小时>
强制性警告:请勿以这种方式使用 AutoAddPolicy
– 您将失去针对 MITM 攻击 这样做.如需正确的解决方案,请参阅Paramiko未知服务器".
Obligatory warning: Do not use AutoAddPolicy
this way – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".
这篇关于“getaddrinfo 失败"使用 Paramiko 连接到 SFTP 服务器时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!