def filterbyasn(ips, max_per_asn, max_total):
    ips_ipv4 = [ip for ip in ips if (ip['net'] == 'ipv4')]
    ips_ipv6 = [ip for ip in ips if (ip['net'] == 'ipv6')]
    ips_onion = [ip for ip in ips if (ip['net'] == 'onion')]
    result = []
    asn_count = {}
    for ip in ips_ipv4:
        if (len(result) == max_total):
            break
        try:
            asn = int([x.to_text() for x in dns.resolver.query(('.'.join(reversed(ip['ip'].split('.'))) + '.origin.asn.cymru.com'), 'TXT').response.answer][0].split('"')[1].split(' ')[0])
            if (asn not in asn_count):
                asn_count[asn] = 0
            if (asn_count[asn] == max_per_asn):
                continue
            asn_count[asn] += 1
            result.append(ip)
        except:
            sys.stderr.write((('ERR: Could not resolve ASN for "' + ip['ip']) + '"\n'))
    result.extend(ips_ipv6)
    result.extend(ips_onion)
    return result
def filterbyasn(ips, max_per_asn, max_total):
    ips_ipv4 = [ip for ip in ips if (ip['net'] == 'ipv4')]
    ips_ipv6 = [ip for ip in ips if (ip['net'] == 'ipv6')]
    ips_onion = [ip for ip in ips if (ip['net'] == 'onion')]
    result = []
    asn_count = {}
    for ip in ips_ipv4:
        if (len(result) == max_total):
            break
        try:
            asn = int([x.to_text() for x in dns.resolver.query(('.'.join(reversed(ip['ip'].split('.'))) + '.origin.asn.cymru.com'), 'TXT').response.answer][0].split('"')[1].split(' ')[0])
            if (asn not in asn_count):
                asn_count[asn] = 0
            if (asn_count[asn] == max_per_asn):
                continue
            asn_count[asn] += 1
            result.append(ip)
        except:
            sys.stderr.write((('ERR: Could not resolve ASN for "' + ip['ip']) + '"\n'))
    result.extend(ips_ipv6)
    result.extend(ips_onion)
    return result