簡體   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