[Tutor] Exception Handling
Nym City
nymcity at yahoo.com
Sat Oct 3 00:57:15 CEST 2015
Hello,
I am trying to get IP addresses for about 500 public domains but I think for some where resolution is not available - my exception handling breaks the code.
To troubleshoot, I added several Print statements to see what I was getting and it looks like my blank list is getting populated with domain names just fine and it starts to resolve addresses but less than half way though it errors out with the following message:
Traceback (most recent call last):
File "C:/Users/Documents/Python/MyProject/HostnameResolution/HostnameResolution.py", line 15, in <module>
ResolveHostname = socket.gethostbyname(name)
socket.gaierror: [Errno 11004] getaddrinfo failed
Here is the Code:
import socket
ListOfHostNames = []
with open('hostnames.csv', 'r') as f:
for line in f:
line = line.strip()
ListOfHostNames.append(line)
newFile = open('hostnames.csv', 'w')
print(ListOfHostNames)
for name in ListOfHostNames:
try:
ResolveHostname = socket.gethostbyname(name)
print(ResolveHostname)
newFile.write(ResolveHostname + "\n")
print(ResolveHostname)
except socket.herror as e:
newFile.write("No resolution available for %s" % (name) + "\n")
newFile.close()
Please advice. Thanks! Thank you.
More information about the Tutor
mailing list