0

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.

dawg
  • 98,345
  • 23
  • 131
  • 206
tafiela
  • 93
  • 2
  • 10
  • "Reverse DNS Lookup" searches for a **domain name** given an **ip**. Your list already contains domains. You don't need reverse dns lookup. – jfs Mar 02 '14 at 17:32

1 Answers1

3

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)))
user273324
  • 152
  • 3
  • 12
  • `for i in file_O:` does read the file line by line. A file is an iterator over lines. You don't need `.readlines()` to read lines. – jfs Mar 02 '14 at 17:29