我正在尝试使用Python对IP进行DNS查找,我正在使用Python 3.x。
我使用一长串网址,如下所示:
yahoo.com
google.com
linkedin.com
facebook.com
cnn.com
foxnews.com
这是我的sc :::
import socket
file = '/Users/Python/Scripts/URL-list.txt'
file_O=open(file, 'r')
for i in file_O:
print(i + socket.gethostbyname('i'))`
出于某种原因,当我一次运行一个URL时,它工作正常但是当我在我的列表上运行它时,它们都返回相同的IP。这是一个例子;
yahoo.com
69.16.143.64
google.com
69.16.143.64
linkedin.com
69.16.143.64
facebook.com
69.16.143.64
cnn.com
69.16.143.64
foxnews.com
69.16.143.64
知道我做错了什么?我猜它在读取文本文件的方式,但是这个IP不会映射到任何或URL。
答案 0 :(得分:3)
然后你想要剥离线然后使用循环,所以这样的东西对你有用:
import socket
file = '/Users/Python/Scripts/URL-list.txt'
f = open(file, 'r')
lines = f.readlines()
f.close()
for i in lines:
host = i.strip()
print("%s - %s" % (host, socket.gethostbyname(host)))