Lewie
Lewie

Reputation: 115

Python Parser to resolve IP address from hostnames in text file

I'm attempting to resolve the hostnames I have in a text file to IPs using sockets.

file.txt:

google.com
yahoo.com
iliketurtles.com

import socket
with open("file.txt", "r") as ins:
  for line in ins:
    print  socket.gethostbyname(line.strip())

I'm getting the following gaierror:

print socket.gethostbyname(line.strip()) socket.gaierror: [Errno 8] nodename nor servname provided, or not known

What do I need to do differently to resolve the hostname on each line of the file and print to standard output?

Thanks in advance!

Upvotes: 0

Views: 936

Answers (1)

Lev Zakharov
Lev Zakharov

Reputation: 2427

This code works for me. Sounds like it's a network issue. Check your internet connection - try to ping this servers and check that your DNS server works correctly.

Upvotes: 1

Related Questions