Question

i am trying to do a DNS lookup to an IP with Python, im using Python 3.x.

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

Was it helpful?

Solution

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)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top