从txt文件读取IP并在python中连接到ftp

从txt文件读取IP并在python中连接到ftp,python,ftplib,Python,Ftplib,我正在尝试用python编写一个脚本,它连接到我们所有的FTP,并告诉我它们已启动,并在连接时列出它们的目录 我将尝试使用一个名为“ips.txt”的文件,其中我们的所有IP都位于其中-每行一个,并使用以下脚本: import socket import ftplib username = "xxx" password = "xxx" for server in open("ips.txt", "r").readlines(): try: ftp = ftplib

我正在尝试用python编写一个脚本,它连接到我们所有的FTP,并告诉我它们已启动,并在连接时列出它们的目录

我将尝试使用一个名为“ips.txt”的文件,其中我们的所有IP都位于其中-每行一个,并使用以下脚本:

import socket
import ftplib

username = "xxx"
password = "xxx"



for server in open("ips.txt", "r").readlines():
    try:
        ftp = ftplib.FTP(server)
        welcome = ftp.getwelcome()
        print (welcome)

        try:
            attempt = ftp.login(user=username, passwd=password)
            success = ("[****] Working " + server + '\n')

            print(success)
            data = []
            ftp.dir(data.append)
            for lines in data:
                print (lines)

        except:
            print (server, username, password)
            pass

    except:
        print ("Timeout...")
但脚本似乎跳过了所有内容,只是打印了“超时…”:(

我是一个该死的python初学者,所以请耐心点

编辑: 移除外部后,请尝试/除非我得到了回溯:

    Traceback (most recent call last):
  File "ftp.py", line 12, in <module>
    ftp = ftplib.FTP(server)
  File "C:\Python3.5.1\lib\ftplib.py", line 118, in __init__
    self.connect(host)
  File "C:\Python3.5.1\lib\ftplib.py", line 153, in connect
    source_address=self.source_address)
  File "C:\Python3.5.1\lib\socket.py", line 693, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Python3.5.1\lib\socket.py", line 732, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

因此,根据您提供的文件,每个IP的新行在进行
readlines
调用时,仍保留每个IP末尾的换行符。这很可能是您获得
GAIRROR
的原因

在我这边复制,使用换行符,我的回溯产生:

>>> FTP('10.10.10.10\n')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ftplib.py", line 118, in __init__
    self.connect(host)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ftplib.py", line 153, in connect
    source_address=self.source_address)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 693, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 732, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
然后,您将去掉IP末尾的
\n
,您至少应该调用正确的IP地址

或者,您可以尝试查看
拆分行
是否适合您,考虑到您正在处理一个IP地址列表,它可能是一个很好的替代方案

将针对
字符串
为您删除换行符,因此您还需要对打开的对象调用
read
。如下所示:

for server in open("ips.txt", "r").read().splitlines():

您是否尝试在每次迭代中打印
server
的内容以查看传递给
FTP
类的内容?能否提供txt文件中的几行作为示例?没有尝试。该文件看起来像:10.10.10.10.10.10.10.10.11 10.10.10.19…删除外部
try/except
,您就会知道会出现什么错误向上。
try/except
s是调试器的噩梦。ip是由空格分隔的?还是由换行符分隔的?您不是在剥离每一行的换行符。执行
server.strip()
FTP(server.strip())
for server in open("ips.txt", "r").read().splitlines():