python代码中的错误阻止成功递归?

时间:2017-07-05 03:14:24

标签: python

我一直在编写一个脚本来摄取一个包含电子邮件地址的文件(accounts.txt),然后根据这些邮件地址对每个文件进行验证,看看它们是否出现在数据转储中。该脚本似乎有效,但是存在一个错误,一旦找到肯定的命中,它将忽略任何其他匹配......

例如; 如果我的" accounts.txt"文件包含以下条目:

a@a.com
b@b.com

即使这两个都应该返回结果,但一旦脚本运行,就会找到a@a.com上的匹配,但是b@b.com不会返回任何内容。

我似乎无法弄清楚为什么会发生这种情况,理想情况下我想要输出所有的命中。

仅供参考,该脚本正在查询“已经完成”和“#39;这是一个用于查找凭据转储中找到的电子邮件地址的站点。

任何寻找我的错误的帮助都将非常感激。以下是我目前的剧本。

#!/usr/bin/env python

import argparse
import json
import requests
import time

breaches_by_date = {}       
breaches_by_account = {}    
breaches_by_name = {}       

class Breach(object):
    def __init__(self, e, n, d):
        self.email = e
        self.name = n
        self.date = d

    def __repr__(self):
        return "%s: %s breached on %s" % (self.email, self.name, self.date)

def accCheck(acc):
    global breaches_by_date, breaches_by_account, breaches_by_name
    r = requests.get('https://haveibeenpwned.com/api/v2/breachedaccount/%s?truncateResponse=false' % acc)
    try:
        data = json.loads(r.text)
    except ValueError:
        print("No breach information for %s" % acc)
        return
    for i in data:
        name, date = (i['Name'], i['BreachDate'])
        breach = Breach(acc, name, date)
        try: breaches_by_account[acc].append(breach) 
        except: breaches_by_account[acc] = [breach]
        try: breaches_by_name[name].append(breach)
        except: breaches_by_name[name] = [breach]
        try: breaches_by_date[date].append(breach)
        except: breaches_by_date[date] = [breach]

def readFromFile(fname="accounts.txt"):
    accounts=[]
    with open(fname, "r+") as f:
        accounts = [l.strip() for l in f.readlines()] 
    return accounts

if __name__ == '__main__':
    accounts = readFromFile()
    for email_addr in accounts:
        accCheck(email_addr)
    print
    print("Breaches by date")
    for date, breaches in breaches_by_date.items():
        for breach in breaches:
            print(breach) 
        print

    print("Breaches by account")
    for acc, breaches in breaches_by_account.items():
        print(acc)
        for breach in breaches:
            print("%s breached on %s" % (breach.name, breach.date)) 
        print

    print("Breaches by name")
    for name, breaches in breaches_by_name.items():
        print("%s breached for the following accounts:" % name)
        for breach in breaches:
            print("%s on %s" % (breach.email, breach.date))
        print

1 个答案:

答案 0 :(得分:0)

我不是百分百肯定知道您的问题来自哪里,但我会选择以下代码:

emails_to_check = open("/path/to/yourfile").read().split("\n")
for email in emails_to_check:
 if is_email_blacklisted(email):
  do_something()