0

我正在尝试使用 Python 对 IP 进行 DNS 查找,我使用的是 Python 3.x。

我使用一长串 URL,如下所示:

yahoo.com
google.com
linkedin.com
facebook.com
cnn.com
foxnews.com

这是我的剧本:

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 或 URL。

4

1 回答 1

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)))
于 2014-03-01T20:27:09.580 回答