Python: ftplib: [Errno 11001] getaddrinfo 失败

Python: ftplib: [Errno 11001] getaddrinfo failed

我在以前没有抛出此异常的系统上维护一个脚本 运行。新固件版本导致引发此异常。例如,nlist 和 retrlines 函数以前工作,现在抛出异常。我尝试过使用多种不同的路径表示等。 [Errno 11004] 有未决问题,但 [Errno 11001] 没有。我已经尝试了其他相关问题的解决方案,但没有成功。谢谢!

>>> from ftplib import FTP
>>> ftp = FTP('192.168.1.11')
>>> ftp.login('username', 'password')  # actual credentials replaced for code sample...
'230 User logged in, proceed'
>>> ftp.nlst()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\some_user\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 557, in nlst
    self.retrlines(cmd, files.append)
  File "C:\Users\some_user\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 466, in retrlines
    with self.transfercmd(cmd) as conn, \
  File "C:\Users\some_user\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 397, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:\Users\some_user\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 359, in ntransfercmd
    source_address=self.source_address)
  File "C:\Users\some_user\AppData\Local\Programs\Python\Python36-32\lib\socket.py", line 704, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\some_user\AppData\Local\Programs\Python\Python36-32\lib\socket.py", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
>>> ftp.retrlines('LIST')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\some_user\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 466, in retrlines
    with self.transfercmd(cmd) as conn, \
  File "C:\Users\some_user\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 397, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:\Users\some_user\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 359, in ntransfercmd
    source_address=self.source_address)
  File "C:\Users\some_user\AppData\Local\Programs\Python\Python36-32\lib\socket.py", line 704, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\some_user\AppData\Local\Programs\Python\Python36-32\lib\socket.py", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
>>>

问题在于新固件包含一个新的 TCP/IP 堆栈(其中包含一个新的 FTP 实现)。这个新实现结果证明不支持被动模式。我不得不关闭被动模式,它在 FTPLIB 中默认启用。一旦我这样做了,一切正常。

        ftp = FTP(client.host)
        ftp.login(self.username, self.password)
        ftp.set_pasv(False)  # bingo :)