本文整理汇总了Python中netaddr.IPSet方法的典型用法代码示例。如果您正苦于以下问题:Python netaddr.IPSet方法的具体用法?Python netaddr.IPSet怎么用?Python netaddr.IPSet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netaddr
的用法示例。
在下文中一共展示了netaddr.IPSet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_ip_ranges
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def parse_ip_ranges(iprangesconfig):
ipranges = netaddr.IPSet([])
iprangessplit = [i.strip() for i in iprangesconfig.split(",")]
for iprange in iprangessplit:
if not iprange:
continue
try:
if "-" in iprange:
spl = iprange.split("-", 1)
ipns = netaddr.IPRange(spl[0], spl[1])
elif "*" in iprange:
ipns = netaddr.IPGlob(iprange)
else:
ipns = netaddr.IPNetwork(iprange)
ipranges.add(ipns)
except:
pprint("Bad IP definition: %s" % iprangesconfig)
sys.exit()
return ipranges
示例2: get_ip_subnets
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def get_ip_subnets(ip):
"""Get a list of subnets contained in the specified subnet.
:type ip: str
:param ip: The IP that subnets will be returned for.
:list[netaddr.IPNetwork]
"""
ip = ip.strip().replace(" ", "")
if "/" in ip:
return [netaddr.IPNetwork(ip)]
elif "-" in ip:
start_ip, end_ip = ip.split("-")
ip_set_object = netaddr.IPSet(netaddr.IPRange(start_ip, end_ip, flags=netaddr.ZEROFILL))
return [address for address in ip_set_object.iter_cidrs()]
else:
if is_ipv4_string(ip):
return [netaddr.IPNetwork(ip)]
else:
raise ValueError("Invalid IP string '{}'.".format(ip))
示例3: parse_idrac_arguments
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def parse_idrac_arguments(idrac_list):
ip_set = netaddr.IPSet()
for idrac in idrac_list:
ip_set = ip_set.union(ip_set_from_idrac(idrac))
return ip_set
示例4: ip_set_from_address_range
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def ip_set_from_address_range(start, end):
try:
start_ip_address = ip_address_from_address(start)
end_ip_address = ip_address_from_address(end)
except (NotSupported, ValueError) as e:
raise ValueError(
('invalid IP range: %(start)s-%(end)s (%(message)s)') %
{
'start': start,
'end': end,
'message': e.message})
except netaddr.AddrFormatError as e:
raise ValueError(
("invalid IP range: '%(start)s-%(end)s' (%(message)s)") %
{
'start': start,
'end': end,
'message': e.message})
if start_ip_address > end_ip_address:
raise ValueError(
('invalid IP range: %(start)s-%(end)s (lower bound IP greater than'
' upper bound)') %
{
'start': start,
'end': end})
ip_range = netaddr.IPRange(start_ip_address, end_ip_address)
return netaddr.IPSet(ip_range)
示例5: ip_set_from_address
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def ip_set_from_address(address):
ip_set = netaddr.IPSet()
try:
ip_address = ip_address_from_address(address)
ip_set.add(ip_address)
except ValueError:
ip_network = ip_network_from_address(address)
ip_set.update(ip_network.iter_hosts())
return ip_set
示例6: _ip_prefix_conflict
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def _ip_prefix_conflict(cls, first_ip_prefix, second_ip_prefix):
if first_ip_prefix is None or second_ip_prefix is None:
return True
first_ipset = netaddr.IPSet([first_ip_prefix])
second_ipset = netaddr.IPSet([second_ip_prefix])
return bool(first_ipset & second_ipset)
示例7: find_unused_ip
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def find_unused_ip(ip_range_begin, ip_range_end, ips_in_use):
candidate = None
in_range = ipset(iprange(ip_range_begin, ip_range_end))
in_use = ipset(ips_in_use)
try:
candidate = str(ipaddr((in_range - in_use).pop()))
except Exception:
LOG.error("Could not allocate IP address for range:{0}-{1}".format(
ip_range_begin, ip_range_end))
finally:
return candidate
示例8: random_address
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def random_address(base):
"""Return a random address based on a base prefix."""
prefix = netaddr.IPNetwork(base)
addresses = netaddr.IPSet(prefix)
for address in [prefix.network, prefix.broadcast]:
addresses.remove(address)
return str(random.choice(list(addresses))) + '/' + str(prefix.prefixlen)
示例9: test_get_ipv6_tenant_subnet_routes_by_bgp_speaker_ipv6
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def test_get_ipv6_tenant_subnet_routes_by_bgp_speaker_ipv6(self):
tenant_cidr = '2001:db8::/64'
binding_cidr = '2001:ab8::/64'
routes = self._advertised_routes_by_bgp_speaker(6, 1234, tenant_cidr,
binding_cidr)
self.assertEqual(1, len(routes))
dest_prefix = routes[0]['destination']
next_hop = routes[0]['next_hop']
self.assertEqual(tenant_cidr, dest_prefix)
self.assertTrue(netaddr.IPSet([binding_cidr]).__contains__(next_hop))
示例10: test_get_ipv4_tenant_subnet_routes_by_bgp_speaker_ipv4
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def test_get_ipv4_tenant_subnet_routes_by_bgp_speaker_ipv4(self):
tenant_cidr = '172.16.10.0/24'
binding_cidr = '20.10.1.0/24'
routes = self._advertised_routes_by_bgp_speaker(4, 1234, tenant_cidr,
binding_cidr)
routes = list(routes)
self.assertEqual(1, len(routes))
dest_prefix = routes[0]['destination']
next_hop = routes[0]['next_hop']
self.assertEqual(tenant_cidr, dest_prefix)
self.assertTrue(netaddr.IPSet([binding_cidr]).__contains__(next_hop))
示例11: targets_to_ip_list
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def targets_to_ip_list(targets):
ipset = IPSet()
for t in targets:
ipset.add(t)
return [str(ip) for ip in ipset]
示例12: as_netaddr_set
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def as_netaddr_set(self):
"""This returns a netaddr set representing the TrafficRange"""
return netaddr.IPSet(self.as_netaddr_obj())
示例13: as_netaddr_set
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def as_netaddr_set(self):
"""This returns a netaddr set representing the Network_Object"""
return netaddr.IPSet(self.as_netaddr_obj())
示例14: get_ip_set
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def get_ip_set(self):
"""
This returns an IPset representing the object.
:rtype: IPSet
"""
return IPSet(self.get_ip_network())
示例15: get_cidrs
# 需要导入模块: import netaddr [as 别名]
# 或者: from netaddr import IPSet [as 别名]
def get_cidrs(size, qty, vpc_id):
# TODO: add locking mechanism to prevent collisions when run concurrently
vpc = ec2_client.Vpc(vpc_id)
allocated_cidrs = netaddr.IPSet([s.cidr_block for s in vpc.subnets.all()])
unused_cidrs = netaddr.IPSet([vpc.cidr_block]) ^ allocated_cidrs
available_cidrs = []
for sl in [list(s.subnet(size)) for s in unused_cidrs.iter_cidrs()]:
available_cidrs = available_cidrs + sl
if len(available_cidrs) < qty:
raise Exception("Not enough available space in the vpc\n")
return [str(s) for s in available_cidrs[-(qty):]]