0

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!

1
  • Or you sure that line.strip() is not empty? Do you have some trailing end of line characters in your file? Also you should look at socket.getaddrinfo() instead of socket.gethostbyname() for all the reasons explained in the socket documentation. Commented Aug 10, 2018 at 21:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I can ping all the hosts in my list and run dig command on them individually. I'm on OSX. What OS are you testing from? Also I'm on Python 2.7
I also use OSX. I tested on both versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.