简体   繁体   中英

How to stop python script from exiting on error

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!

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()
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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM