我正在尝试读取纯文本文件并解析每个IP地址,并且(现在)只是将它们吐出屏幕。
import socket
f = open("test.txt")
num_line = sum(1 for line in f)
f.close()
with open("test.txt", "r") as ins:
array = []
for line in ins:
array.append(line)
for i in range(0,num_line):
x = array[i]
print x
data = socket.gethostbyname_ex(x)
print data
目前我收到以下内容:
me@v:/home/# python resolve-list2.py
test.com
Traceback (most recent call last):
File "resolve-list2.py", line 15, in <module>
data = socket.gethostbyname_ex(x)
socket.gaierror: [Errno -2] Name or service not known
谷歌搜索该错误似乎没有帮助我...... 文本文件目前只包含一行(test.com),但即使有多行/不同的主机,我也会得到同样的错误。
有什么建议吗?
谢谢!
答案 0 :(得分:26)
import socket
with open("test.txt", "r") as ins:
for line in ins:
print socket.gethostbyname(line.strip())