繁体   English   中英

如何阻止python脚本错误退出

[英]How to stop python script from exiting on error

我编写了一个小的python脚本,使用pythonwhois进行了一些域的批量whois检查。

该脚本从testdomains.txt中读取域,并一一检查。 然后,它将有关域的一些信息记录到results.txt中

这是我的脚本:

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

当未注册域或Whois服务器由于某种原因无法答复时,会出现我的问题。 脚本退出如下:

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__'

我的问题是,如何避免整个脚本退出?

如果发生错误,我希望脚本跳至下一个域并继续运行而不退出。 将错误记录到results.txt肯定也很好。

谢谢!

您想在这里使用try/except进行异常处理。

此处阅读有关异常处理的文档

摘录感兴趣的代码段,您可以将调用包装在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()

有两种方法:1.)可以删除易碎的代码以确保不会发生期望。 例:

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.)优雅地处理异常,并让代码继续。 如上所述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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