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!
line.strip()is not empty? Do you have some trailing end of line characters in your file? Also you should look atsocket.getaddrinfo()instead ofsocket.gethostbyname()for all the reasons explained in thesocketdocumentation.