Python netaddr 模块,all_matching_cidrs() 实例源码

我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用netaddr.all_matching_cidrs()

项目:Taigabot    作者:FrozenPigs    | 项目源码 | 文件源码
def test_all_matching_cidrs_v6():
    assert all_matching_cidrs('::ffff:192.0.2.1', ['::ffff:192.0.2.0/96']) == [IPNetwork('::ffff:192.0.2.0/96')]
    assert all_matching_cidrs('::192.0.2.1', ['::192.0.2.0/96']) == [IPNetwork('::192.0.2.0/96')]
    assert all_matching_cidrs('::192.0.2.1', ['192.0.2.0/23']) == []
    assert all_matching_cidrs('::192.0.2.1', ['192.0.2.0/24', '::192.0.2.0/120']) == [IPNetwork('::192.0.2.0/120')]
    assert all_matching_cidrs('::192.0.2.1', [IPNetwork('192.0.2.0/24'), IPNetwork('::192.0.2.0/120')]) == [IPNetwork('::192.0.2.0/120')]
项目:mass-ipv4-whois    作者:marklit    | 项目源码 | 文件源码
def in_known_cidr_block(ip_address):
    redis_con = redis.StrictRedis(host=settings.REDIS_HOST,
                                  port=settings.REDIS_PORT,
                                  db=settings.REDIS_DB)
    cidrs = redis_con.get('cidrs')

    if not cidrs or not len(cidrs):
        return False

    return len(netaddr.all_matching_cidrs(ip_address, cidrs.split(','))) > 0
项目:event-driven-security    作者:acantril    | 项目源码 | 文件源码
def test_all_matching_cidrs_v6():
    assert all_matching_cidrs('::ffff:192.0.2.1', ['::ffff:192.0.2.0/96']) == [IPNetwork('::ffff:192.0.2.0/96')]
    assert all_matching_cidrs('::192.0.2.1', ['::192.0.2.0/96']) == [IPNetwork('::192.0.2.0/96')]
    assert all_matching_cidrs('::192.0.2.1', ['192.0.2.0/23']) == []
    assert all_matching_cidrs('::192.0.2.1', ['192.0.2.0/24', '::192.0.2.0/120']) == [IPNetwork('::192.0.2.0/120')]
    assert all_matching_cidrs('::192.0.2.1', [IPNetwork('192.0.2.0/24'), IPNetwork('::192.0.2.0/120')]) == [IPNetwork('::192.0.2.0/120')]
项目:networking-h3c    作者:openstack    | 项目源码 | 文件源码
def _check_for_dup_router_subnet(self, context, router,
                                     network_id, subnet_id, subnet_cidr):
        try:
            # It's possible these ports are on the same network, but
            # different subnets.
            new_ipnet = netaddr.IPNetwork(subnet_cidr)
            for p in (rp.port for rp in router.attached_ports):
                for ip in p['fixed_ips']:
                    if ip['subnet_id'] == subnet_id:
                        msg = (_("Router already has a port on subnet %s")
                               % subnet_id)
                        raise common_exceptions.BadRequest(
                            resource='router', msg=msg)
                    # Ignore temporary Prefix Delegation CIDRs
                    if subnet_cidr == q_const.PROVISIONAL_IPV6_PD_PREFIX:
                        continue
                    sub_id = ip['subnet_id']
                    cidr = self._core_plugin.get_subnet(context.elevated(),
                                                        sub_id)['cidr']
                    ipnet = netaddr.IPNetwork(cidr)
                    match1 = netaddr.all_matching_cidrs(new_ipnet, [cidr])
                    match2 = netaddr.all_matching_cidrs(ipnet, [subnet_cidr])
                    if match1 or match2:
                        data = {'subnet_cidr': subnet_cidr,
                                'subnet_id': subnet_id,
                                'cidr': cidr,
                                'sub_id': sub_id}
                        msg = (_("Cidr %(subnet_cidr)s of subnet "
                                 "%(subnet_id)s overlaps with cidr %(cidr)s "
                                 "of subnet %(sub_id)s") % data)
                        raise common_exceptions.BadRequest(
                            resource='router', msg=msg)
        except exc.NoResultFound:
            pass
项目:aCloudGuru-Event-Driven-Security    作者:mikegchambers    | 项目源码 | 文件源码
def test_all_matching_cidrs_v6():
    assert all_matching_cidrs('::ffff:192.0.2.1', ['::ffff:192.0.2.0/96']) == [IPNetwork('::ffff:192.0.2.0/96')]
    assert all_matching_cidrs('::192.0.2.1', ['::192.0.2.0/96']) == [IPNetwork('::192.0.2.0/96')]
    assert all_matching_cidrs('::192.0.2.1', ['192.0.2.0/23']) == []
    assert all_matching_cidrs('::192.0.2.1', ['192.0.2.0/24', '::192.0.2.0/120']) == [IPNetwork('::192.0.2.0/120')]
    assert all_matching_cidrs('::192.0.2.1', [IPNetwork('192.0.2.0/24'), IPNetwork('::192.0.2.0/120')]) == [IPNetwork('::192.0.2.0/120')]