返回计数而不是使用全局变量的更好方法

时间:2019-02-22 18:49:35

标签: python dns spf

我有以下代码(使用dnspython)可以正常工作-但它使用的是我不热衷的全局变量。我当时以为可以使用递归函数,但是没有明显的终点。

有人对如何改进它有任何想法吗?

import dns.resolver

dns_resolver = dns.resolver.Resolver()
dns_resolver.nameservers = ['1.1.1.1', '1.0.0.1']

resolve_count = 0


def get_spf_count(domain_name):
    global resolve_count

    for answer in dns_resolver.query(domain_name, 'TXT'):
        spf = answer.to_text() if 'v=spf1' in answer.to_text() else None

        if spf:
            spf_records = [
                record
                for record in spf.replace('" "', '').replace('"', '').split()
                if record not in ['v=spf1', '~all', '-all', '+all', '?all']
            ]
            for record in spf_records:
                if 'include:' in record:
                    check_domain = record.split(':')[1]
                    get_spf_count(check_domain)
                    resolve_count += 1
                elif record.startswith(('a:', 'mx:', 'ptr:', 'exists:')):
                    resolve_count += 1

get_spf_count('google.com')
print(resolve_count)

2 个答案:

答案 0 :(得分:1)

这是带有适当局部变量的稍微清理的递归函数。

import dns.resolver


def get_spf_count(domain_name, dns_resolver=None):
    if dns_resolver is None:
        dns_resolver = dns.resolver.Resolver()
        dns_resolver.nameservers = ['1.1.1.1', '1.0.0.1']

    resolve_count = 0

    for answer in dns_resolver.query(domain_name, 'TXT'):
        spf = answer.to_text() if 'v=spf1' in answer.to_text() else None

        if spf:
            spf_records = [
                record
                for record in spf.replace('" "', '').replace('"', '').split()
                if record not in ['v=spf1', '~all', '-all', '+all', '?all']
            ]
            for record in spf_records:
                if 'include:' in record:
                    check_domain = record.split(':')[1]
                    resolve_count += 1 + get_spf_count(check_domain, dns_resolver)
                elif record.startswith(('a:', 'mx:', 'ptr:', 'exists:')):
                    resolve_count += 1

    return resolve_count


print(get_spf_count('google.com'))

请注意,函数所需的所有内容如何都在函数内部,包括dns.resolver.Resolver()对象(以及如果需要的话,如何传递共享的解析程序对象)。

答案 1 :(得分:0)

为什么不将resolve_count作为变量传递,而让函数返回更新后的值?

def get_spf_count(domain_name, resolve_count):
    for answer in dns_resolver.query(domain_name, 'TXT'):
        spf = answer.to_text() if 'v=spf1' in answer.to_text() else None
        if spf:
            spf_records = [
                record
                for record in spf.replace('" "', '').replace('"', '').split()
                if record not in ['v=spf1', '~all', '-all', '+all', '?all']
            ]
            for record in spf_records:
                if 'include:' in record:
                    check_domain = record.split(':')[1]
                    get_spf_count(check_domain, resolve_count)
                    resolve_count += 1
                elif record.startswith(('a:', 'mx:', 'ptr:', 'exists:')):
                    resolve_count += 1

    return resolve_count    


resolve_count = get_spf_count('google.com', 0)
print(resolve_count)