简体   繁体   English

从值列表中反向DNS查找-Python

[英]Reverse DNS Lookup from a list of values - Python

i am trying to do a DNS lookup to an IP with Python, im using Python 3.x. 我正在尝试使用Python(使用Python 3.x)对IP进行DNS查找。

im using a long list of URLs, which look like : 即时通讯使用一长串网址,如下所示:

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

here is my scirpt : 这是我的脚本:

import socket

file = '/Users/Python/Scripts/URL-list.txt'

file_O=open(file, 'r')

for i in file_O:
    print(i + socket.gethostbyname('i'))`

for some reason, when i run it for a URL at a time, it works perfect but when i run it on my list, they all return the same IP. 由于某种原因,当我一次为一个URL运行它时,它可以完美运行,但是当我在列表中运行它时,它们都返回相同的IP。 here is an example; 这是一个例子;

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

any idea what am i doing wrong? 知道我在做什么错吗? im guessing its in the way its read the text file, but then this IP doesnt map to any or the URLs. 即时通讯猜测其读取文本文件的方式,但此IP不会映射到任何URL。

Then you'd want to strip the line then use the loop, so something like this would work for you: 然后,您要剥离线,然后使用循环,这样类似的事情将为您工作:

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)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM