当前位置: 首页>>代码示例>>Python>>正文


Python netaddr.IPSet类代码示例

本文整理汇总了Python中netaddr.IPSet的典型用法代码示例。如果您正苦于以下问题:Python IPSet类的具体用法?Python IPSet怎么用?Python IPSet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了IPSet类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: isWhitelisted

    def isWhitelisted(self,conn,indicatorType,indicator):
        """Return whether or not the indicator of type indicatorType is whitelisted by this whitelist.
        If the indicator is a single address, it is whitelisted if it is included in any CIDR.
        If the indicator is a network spec, it is whitelisted if all of the addresses it represents are included in any CIDR
        """
        sn=IPNetwork(indicator)
        minip=sn.first
        maxip=sn.last

        c=conn.cursor()
        c.execute("select cidr,minip,maxip from ipv4sn where ? between minip and maxip or ? between minip and maxip order by minip",(minip,maxip))
        ipset=None
        rec=c.fetchone()
        # create a set of all ips contained network specs that contain the min and max ip specified by the indicator
        while (rec != None):
            if(ipset==None):
                ipset = IPSet(IPNetwork(rec[0]))
            else:
                ipset = ipset | IPSet(IPNetwork(rec[0]))
            rec=c.fetchone()
        # if the resulting set is empty, the indicator is not whitelisted
        if(ipset == None):
            return False
        # if the set of IPs represented by the indicator is a subset of the IPs set created above, then it is whitelisted
        ips=IPSet(sn)
        if(ips.issubset(ipset)):
            rv=True
        else:
            rv=False
        c.close()
        return rv
开发者ID:anl-cyberscience,项目名称:LQMToolset,代码行数:31,代码来源:ipv4snwl.py

示例2: test_ipset_basic_api

def test_ipset_basic_api():
    range1 = IPRange('192.0.2.1', '192.0.2.15')

    ip_list = [
        IPAddress('192.0.2.1'),
        '192.0.2.2/31',
        IPNetwork('192.0.2.4/31'),
        IPAddress('192.0.2.6'),
        IPAddress('192.0.2.7'),
        '192.0.2.8',
        '192.0.2.9',
        IPAddress('192.0.2.10'),
        IPAddress('192.0.2.11'),
        IPNetwork('192.0.2.12/30'),
    ]

    set1 = IPSet(range1.cidrs())

    set2 = IPSet(ip_list)

    assert set2 == IPSet([
        '192.0.2.1/32',
        '192.0.2.2/31',
        '192.0.2.4/30',
        '192.0.2.8/29',
    ])

    assert set1 == set2
    assert set2.pop() in set1
    assert set1 != set2
开发者ID:drkjam,项目名称:netaddr,代码行数:30,代码来源:test_ip_sets.py

示例3: test_ipset_converts_to_cidr_networks_v4

def test_ipset_converts_to_cidr_networks_v4():
    s1 = IPSet(IPNetwork('10.1.2.3/8'))
    s1.add(IPNetwork('192.168.1.2/16'))
    assert list(s1.iter_cidrs()) == [
        IPNetwork('10.0.0.0/8'),
        IPNetwork('192.168.0.0/16'),
    ]
开发者ID:drkjam,项目名称:netaddr,代码行数:7,代码来源:test_ip_sets.py

示例4: test_ipset_converts_to_cidr_networks_v6

def test_ipset_converts_to_cidr_networks_v6():
    s1 = IPSet(IPNetwork('fe80::4242/64'))
    s1.add(IPNetwork('fe90::4343/64'))
    assert list(s1.iter_cidrs()) == [
        IPNetwork('fe80::/64'),
        IPNetwork('fe90::/64'),
    ]
开发者ID:drkjam,项目名称:netaddr,代码行数:7,代码来源:test_ip_sets.py

示例5: test_ipset_clear

def test_ipset_clear():
    ipset = IPSet(['10.0.0.0/16'])
    ipset.update(IPRange('10.1.0.0', '10.1.255.255'))
    assert ipset == IPSet(['10.0.0.0/15'])

    ipset.clear()
    assert ipset == IPSet([])
开发者ID:drkjam,项目名称:netaddr,代码行数:7,代码来源:test_ip_sets.py

示例6: ipranges

def ipranges(ip_input):
    ips = []
    ip_input = ip_input.replace(" ", "")
    if '/' in ip_input:
        if ',' in ip_input:
            ip_input_ranges = ip_input.split(',')
            for ip_range in ip_input_ranges:
                for ip in IPSet([ip_range]):
                    ips.append(ip)
        else:
            ips = IPSet([ip_input])
    elif '-' in ip_input:
        if ',' in ip_input:
            ip_input_ranges = ip_input.split(',')
            for ip_range in ip_input_ranges:
                ip_split = ip_range.split("-")
                ip_range_temp = IPRange(ip_split[0], ip_split[1])
                for idx, ip in enumerate(ip_range_temp):
                    ips.append(ip_range_temp[idx])
        else:
            ip_split = ip_input.split("-")
            ips = IPRange(ip_split[0], ip_split[1])
    else:
        ips = IPAddress(ip_input)
    return ips
开发者ID:jkozakow,项目名称:Check_SSL,代码行数:25,代码来源:certcheck.py

示例7: assign_ips

def assign_ips(_upper_ref, _from_key, lower_refs, to_key,
               ip_start='192.168.0.1', ip_end='192.168.0.254',
               **_kwargs):
    """Assign ips to hosts' configurations."""
    if not ip_start or not ip_end:
        return {}
    host_ips = {}
    unassigned_hosts = []
    ips = IPSet(IPRange(ip_start, ip_end))
    for lower_key, lower_ref in lower_refs.items():
        ip_addr = lower_ref.get(to_key, '')
        if ip_addr:
            host_ips[lower_key] = ip_addr
            ips.remove(ip_addr)
        else:
            unassigned_hosts.append(lower_key)

    for ip_addr in ips:
        if not unassigned_hosts:
            break

        host = unassigned_hosts.pop(0)
        host_ips[host] = str(ip_addr)

    logging.debug('assign %s: %s', to_key, host_ips)
    return host_ips
开发者ID:yosshy,项目名称:compass-core,代码行数:26,代码来源:config_merger_callbacks.py

示例8: test_ipset_updates

def test_ipset_updates():
    s1 = IPSet(['192.0.2.0/25'])
    s2 = IPSet(['192.0.2.128/25'])

    s1.update(s2)
    assert s1 == IPSet(['192.0.2.0/24'])

    s1.update(['192.0.0.0/24', '192.0.1.0/24', '192.0.3.0/24'])
    assert s1 == IPSet(['192.0.0.0/22'])
开发者ID:drkjam,项目名称:netaddr,代码行数:9,代码来源:test_ip_sets.py

示例9: generateIP

    def generateIP(self):
        network = IPSet(IPNetwork(self.cidr))
        network.remove(min(network))
        network.remove(max(network))
        hostlist = IPSet([ h.ip for h in self.hosts.all() ])
        available = network - hostlist
        return min(available)

        '''
开发者ID:aloktiagi,项目名称:SDN-IVN,代码行数:9,代码来源:models.py

示例10: test_ipset_exceptions

def test_ipset_exceptions():
    s1 = IPSet(['10.0.0.1'])

    #   IPSet objects are not hashable.
    with pytest.raises(TypeError):
        hash(s1)

    #   Bad update argument type.
    with pytest.raises(TypeError):
        s1.update(42)
开发者ID:drkjam,项目名称:netaddr,代码行数:10,代码来源:test_ip_sets.py

示例11: test_disjointed_ipsets

def test_disjointed_ipsets():
    s1 = IPSet(['192.0.2.0', '192.0.2.1', '192.0.2.2'])
    s2 = IPSet(['192.0.2.2', '192.0.2.3', '192.0.2.4'])

    assert s1 & s2 == IPSet(['192.0.2.2/32'])
    assert not s1.isdisjoint(s2)

    s3 = IPSet(['192.0.2.0', '192.0.2.1'])
    s4 = IPSet(['192.0.2.3', '192.0.2.4'])

    assert s3 & s4 == IPSet([])
    assert s3.isdisjoint(s4)
开发者ID:drkjam,项目名称:netaddr,代码行数:12,代码来源:test_ip_sets.py

示例12: optimize_network_range

def optimize_network_range(ipstr, threshold=0.9, verbose=DEBUG):
    """
    Parses the input string and then calculates the subnet usage percentage. If over
    the threshold it will return a loose result, otherwise it returns strict.

    :param ipstr:
        IP string to be parsed.

    :param threshold:
        The percentage of the network usage required to return a loose result.

    :param verbose:
        Toggle verbosity.

    Example of default behavior using 0.9 (90% usage) threshold:
        >>> import cidrize
        >>> cidrize.optimize_network_range('10.20.30.40-50', verbose=True)
        Subnet usage ratio: 0.34375; Threshold: 0.9
        Under threshold, IP Parse Mode: STRICT
        [IPNetwork('10.20.30.40/29'), IPNetwork('10.20.30.48/31'), IPNetwork('10.20.30.50/32')]

    Example using a 0.3 (30% threshold):
        >>> import cidrize
        >>> cidrize.optimize_network_range('10.20.30.40-50', threshold=0.3, verbose=True)
        Subnet usage ratio: 0.34375; Threshold: 0.3
        Over threshold, IP Parse Mode: LOOSE
        [IPNetwork('10.20.30.32/27')]

    """
    if threshold > 1 or threshold < 0:
        raise CidrizeError('Threshold must be from 0.0 to 1.0')

    # Can't optimize 0.0.0.0/0!
    if ipstr in EVERYTHING:
        return cidrize(ipstr)

    loose = IPSet(cidrize(ipstr))
    strict = IPSet(cidrize(ipstr, strict=True))
    ratio = float(len(strict)) / float(len(loose))

    if verbose:
        print 'Subnet usage ratio: %s; Threshold: %s' % (ratio, threshold)

    if ratio >= threshold:
        if verbose:
            print 'Over threshold, IP Parse Mode: LOOSE'
        result = loose.iter_cidrs()
    else:
        if verbose:
            print 'Under threshold, IP Parse Mode: STRICT'
        result = strict.iter_cidrs()

    return result
开发者ID:secure411dotorg,项目名称:cidrize,代码行数:53,代码来源:cidrize.py

示例13: parse

    def parse(self, data):
        mynets = IPSet()

        for line in data.split("\n"):
            if not line or line[0] == ";":
                continue

            ip, sbl = line.split(";")
            ip = IPNetwork(ip.strip())
            mynets.add(ip)

        return mynets
开发者ID:4sp1r3,项目名称:prelude-correlator,代码行数:12,代码来源:spamhausdrop.py

示例14: concat_networks

def concat_networks(context, pool_1, pool_2):
    if pool_1.is_free and pool_2.is_free:
        network_1 = pool_to_network(pool_1)
        network_2 = pool_to_network(pool_2)
        if network_1.size == network_2.size:
            ipset = IPSet([network_1, network_2])
            cidr = ipset.iter_cidrs()[0]
            pool_1.ip = cidr.first
            pool_1.netmask = cidr.netmask.value
            count = len(pool_to_network(pool_1))
            pool_1.count = count
            pool_delete(context, pool_2.pool_id)
            concat_pool(context, pool_1)
开发者ID:c-los,项目名称:hippools,代码行数:13,代码来源:api.py

示例15: add_available_prefixes

def add_available_prefixes(parent, prefix_list):
    """
    Create fake Prefix objects for all unallocated space within a prefix.
    """

    # Find all unallocated space
    available_prefixes = IPSet(parent) ^ IPSet([p.prefix for p in prefix_list])
    available_prefixes = [Prefix(prefix=p) for p in available_prefixes.iter_cidrs()]

    # Concatenate and sort complete list of children
    prefix_list = list(prefix_list) + available_prefixes
    prefix_list.sort(key=lambda p: p.prefix)

    return prefix_list
开发者ID:DOOMexe,项目名称:netbox,代码行数:14,代码来源:views.py


注:本文中的netaddr.IPSet类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。