I wrote a small python script to do a bulk whois check of some domains using pythonwhois to do the checking.
The script reads domains from testdomains.txt and checks them one by one. Then it logs some information regarding the domain to results.txt
This is my script:
from time import sleep
import pythonwhois
def lookup(domain):
sleep(5)
response = pythonwhois.get_whois(domain)
ns = response['nameservers']
return ns
with open("testdomains.txt") as infile:
domainfile = open('results.txt','w')
for domain in infile:
ns = (lookup(domain))
domainfile.write(domain.rstrip() + ',' + ns+'\n')
domainfile.close()
My problem arises when a domain is not registered or when the whois server fails to reply for some reason. The script exits like this:
Traceback (most recent call last):
File "test8.py", line 17, in <module>
ns = lookup(domain)
File "test8.py", line 9, in lookup
ns = response['nameservers']
TypeError: 'NoneType' object has no attribute '__getitem__'
My question is, what can I do to avoid the entire script from exiting?
In case of an error, I would like the script to jump to the next domain and keep running and not exit. Logging the error to results.txt would definitely be good too.
Thanks!