Reputation: 159
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!
Upvotes: 2
Views: 3718
Reputation: 206
There are two ways: 1.) Either you can remove the brittle code to make sure expection doesn't occur. Example:
from time import sleep
import pythonwhois
def lookup(domain):
sleep(5)
response = pythonwhois.get_whois(domain)
ns = response.get('nameservers')
return ns
with open("testdomains.txt") as infile:
domainfile = open('results.txt','w')
for domain in infile:
ns = (lookup(domain))
if ns:
domainfile.write(domain.rstrip() + ',' + ns+'\n')
domainfile.close()
2.) Handle exception gracefully and let code to continue. As suggested above.
Upvotes: 0
Reputation: 26570
You want to make use of exception handling here with a try/except
.
Read the documentation on exception handling here
Taking the snippet of code of interest, you wrap your call inside a try:
for domain in infile:
try:
ns = lookup(domain)
except TypeError as e:
# should probably use a logger here instead of print
print('domain not found: {}'.format(e))
print('Continuing...')
domainfile.write(domain.rstrip() + ',' + ns+'\n')
domainfile.close()
Upvotes: 4
Reputation: 2501
with open("testdomains.txt") as infile:
domainfile = open('results.txt','w')
for domain in infile:
try:
ns = (lookup(domain))
domainfile.write(domain.rstrip() + ',' + ns+'\n')\
except TypeError:
pass
domainfile.close()
Upvotes: 0