本文整理汇总了Python中ipaddress.IPv6Address方法的典型用法代码示例。如果您正苦于以下问题:Python ipaddress.IPv6Address方法的具体用法?Python ipaddress.IPv6Address怎么用?Python ipaddress.IPv6Address使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ipaddress
的用法示例。
在下文中一共展示了ipaddress.IPv6Address方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_next_hop_ordering
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def test_next_hop_ordering():
nhop1 = next_hop.NextHop(None, None)
nhop2 = next_hop.NextHop("if1", None)
nhop3 = next_hop.NextHop("if1", ipaddress.IPv4Address("1.1.1.1"))
nhop4 = next_hop.NextHop("if1", ipaddress.IPv4Address("2.2.2.2"))
nhop5 = next_hop.NextHop("if2", ipaddress.IPv4Address("1.1.1.1"))
nhop6 = next_hop.NextHop("if2", ipaddress.IPv6Address("1111:1111::"))
assert nhop1 < nhop2
assert nhop2 < nhop3
assert nhop3 < nhop4
assert nhop4 < nhop5
assert nhop5 < nhop6
# pylint:disable=unneeded-not
assert not nhop2 < nhop1
assert not nhop3 < nhop2
assert not nhop4 < nhop3
assert not nhop5 < nhop4
assert not nhop6 < nhop5
示例2: __lt__
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def __lt__(self, other):
# String is not comparable with None
if (self.interface is None) and (other.interface is not None):
return True
if (self.interface is not None) and (other.interface is None):
return False
if self.interface < other.interface:
return True
if self.interface > other.interface:
return False
# Address is not comparable with None
if (self.address is None) and (other.address is not None):
return True
if (self.address is not None) and (other.address is None):
return False
# Address of different address families are not comparable
if (isinstance(self.address, ipaddress.IPv4Address) and
isinstance(other.address, ipaddress.IPv6Address)):
return True
if (isinstance(self.address, ipaddress.IPv6Address) and
isinstance(other.address, ipaddress.IPv4Address)):
return False
return self.address < other.address
示例3: bind_unused_port
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def bind_unused_port(sock: socket.socket, host: Union[str, ipaddress.IPv4Address, ipaddress.IPv6Address] = 'localhost') -> int:
"""Bind the socket to a free port and return the port number.
This code is based on the code in the stdlib's test.test_support module."""
if sock.family in (socket.AF_INET, socket.AF_INET6) and sock.type == socket.SOCK_STREAM:
if hasattr(socket, "SO_EXCLUSIVEADDRUSE"):
with contextlib.suppress(socket.error):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
if not isinstance(host, str):
host = str(host)
if sock.family == socket.AF_INET:
if host == 'localhost':
sock.bind(('127.0.0.1', 0))
else:
sock.bind((host, 0))
elif sock.family == socket.AF_INET6:
if host == 'localhost':
sock.bind(('::1', 0, 0, 0))
else:
sock.bind((host, 0, 0, 0))
else:
raise CommunicationError("unsupported socket family: " + str(sock.family))
return sock.getsockname()[1]
示例4: is_ipv6
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def is_ipv6(self):
"""Boolean: URL network location is an IPv6 address, not a domain?"""
# fix urlparse exception
parsed = urlparse(iocextract.refang_url(self.artifact))
# Handle RFC 2732 IPv6 URLs with and without port, as well as non-RFC IPv6 URLs
if ']:' in parsed.netloc:
ipv6 = ':'.join(parsed.netloc.split(':')[:-1])
else:
ipv6 = parsed.netloc
try:
ipaddress.IPv6Address(ipv6.replace('[', '').replace(']', ''))
except ValueError:
return False
return True
示例5: test_ip
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def test_ip(self):
"""
Returns IP patterns.
"""
rv = extract_ids(CERT_EVERYTHING)
assert [
DNSPattern(pattern=b"service.identity.invalid"),
DNSPattern(pattern=b"*.wildcard.service.identity.invalid"),
DNSPattern(pattern=b"service.identity.invalid"),
DNSPattern(pattern=b"single.service.identity.invalid"),
IPAddressPattern(pattern=ipaddress.IPv4Address(u"1.1.1.1")),
IPAddressPattern(pattern=ipaddress.IPv6Address(u"::1")),
IPAddressPattern(pattern=ipaddress.IPv4Address(u"2.2.2.2")),
IPAddressPattern(pattern=ipaddress.IPv6Address(u"2a00:1c38::53")),
] == rv
示例6: unconfigure_route_ref
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def unconfigure_route_ref(self, conf_obj, path, **kwargs):
paths = self._path_population([path], kwargs['device'])
# find position that neighbor (ip) sit
# replace ip string to IPv4Address object
for path in paths:
ipv4_index_list = [path.index(val) for val in path if '.' in str(val)]
ipv6_index_list = [path.index(val) for val in path if ':' in str(val)]
for index in ipv4_index_list:
path[index] = IPv4Address(path[index])
for index in ipv6_index_list:
path[index] = IPv6Address(path[index])
config = '\n'.join([str(conf_path) for conf_path in paths])
log.info('With following configuration:\n{c}'
.format(c=config))
Configure.conf_configure(device=kwargs['device'],
conf=conf_obj,
conf_structure=paths,
unconfig=True)
示例7: insert_network
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def insert_network(self, prefix, data_offset, strict = True):
self.entries_count += 1
ipnet = ipaddress.ip_network(prefix, strict=False)
if ipnet.version == 6 and self.ip_version != 6:
raise Exception('Encoder: insert_network: cannot add IPv6 address in IPv4 table')
if ipnet.version == 4 and self.ip_version == 6:
base4in6 = ipaddress.IPv6Address(u'::ffff:0:0')
v4in6addr = ipaddress.IPv6Address(int(ipnet.network_address)+int(base4in6))
# Maxmind DBs skips the first 96 bits (do not include the 0xffff)
if self.compat:
v4in6addr = ipaddress.IPv6Address(int(ipnet.network_address))
v4in6addr_plen = ipnet.prefixlen + 96
ipnet = ipaddress.IPv6Network(u'{}/{}'.format(str(v4in6addr), v4in6addr_plen), strict=False)
#print(ipnet)
self.add_to_trie(ipnet, data_offset, strict=strict)
示例8: __init__
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def __init__(self, value):
if not isinstance(
value,
(
ipaddress.IPv4Address,
ipaddress.IPv6Address,
ipaddress.IPv4Network,
ipaddress.IPv6Network
)
):
raise TypeError(
"value must be an instance of ipaddress.IPv4Address, "
"ipaddress.IPv6Address, ipaddress.IPv4Network, or "
"ipaddress.IPv6Network"
)
self._value = value
示例9: get_ip_for_interface
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def get_ip_for_interface(interface_name, ipv6=False):
"""
Get the ip address in IPv4 or IPv6 for a specific network interface
:param str interace_name: declares the network interface name for which the ip should be accessed
:param bool ipv6: Boolean indicating if the ipv6 address should be rertrieved
:return: (str ipaddress, byte ipaddress_bytes) returns a tuple with the ip address as a string and in bytes
"""
addresses = netifaces.ifaddresses(interface_name)
if netifaces.AF_INET6 in addresses and ipv6:
# Use the normal ipv6 address
addr = addresses[netifaces.AF_INET6][0]['addr'].split('%')[0]
bytes_addr = ipaddress.IPv6Address(addr).packed
elif netifaces.AF_INET in addresses and not ipv6:
addr = addresses[netifaces.AF_INET][0]['addr']
bytes_addr = socket.inet_aton(addr)
else:
addr = None
bytes_addr = None
return addr, bytes_addr
示例10: __init__
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def __init__(self, host, port=None, key_file=None, cert_file=None, timeout=None, source_address=None,
*, context=None, check_hostname=None, interface_name=None):
if interface_name is not None:
if '%' not in host:
if isinstance(ipaddress.ip_address(host), ipaddress.IPv6Address):
host = host + '%' + interface_name
if timeout is None:
timeout = socket.getdefaulttimeout()
super(HTTPSConnectionAWDL, self).__init__(host=host, port=port, key_file=key_file, cert_file=cert_file,
timeout=timeout, source_address=source_address, context=context,
check_hostname=check_hostname)
self.interface_name = interface_name
self._create_connection = self.create_connection_awdl
示例11: is_ipv6
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def is_ipv6(string):
"""
Checks if a string is a valid ip-address (v6)
:param string: String to check
:type string: str
:return: True if an ipv6, false otherwise.
:rtype: bool
"""
try:
ipaddress.IPv6Address(string)
return True
except ipaddress.AddressValueError:
return False
示例12: check_ipv6_availability
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def check_ipv6_availability():
print("Проверка работоспособности IPv6", end='')
v6addr = _get_a_record("ipv6.icanhazip.com", "AAAA")
if (v6addr):
v6 = _get_url("http://ipv6.icanhazip.com/", ip=v6addr[0])
if len(v6[1]):
v6src = v6[1].strip()
if force_ipv6 or (not ipaddress.IPv6Address(v6src).teredo
and not ipaddress.IPv6Address(v6src).sixtofour):
print(": IPv6 доступен!")
return v6src
else:
print (": обнаружен туннель Teredo или 6to4, игнорируем.")
return False
print(": IPv6 недоступен.")
return False
示例13: testTeredo
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def testTeredo(self):
# stolen from wikipedia
server = ipaddress.IPv4Address('65.54.227.120')
client = ipaddress.IPv4Address('192.0.2.45')
teredo_addr = '2001:0000:4136:e378:8000:63bf:3fff:fdd2'
self.assertEqual((server, client),
ipaddress.ip_address(teredo_addr).teredo)
bad_addr = '2000::4136:e378:8000:63bf:3fff:fdd2'
self.assertFalse(ipaddress.ip_address(bad_addr).teredo)
bad_addr = '2001:0001:4136:e378:8000:63bf:3fff:fdd2'
self.assertFalse(ipaddress.ip_address(bad_addr).teredo)
# i77
teredo_addr = ipaddress.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1')
self.assertEqual((ipaddress.IPv4Address('94.245.121.253'),
ipaddress.IPv4Address('95.26.244.94')),
teredo_addr.teredo)
示例14: test_next_hop_str
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def test_next_hop_str():
nhop = next_hop.NextHop("if1", ipaddress.IPv4Address("1.2.3.4"))
assert str(nhop) == "if1 1.2.3.4"
nhop = next_hop.NextHop("if1", ipaddress.IPv6Address("1111:1111::"))
assert str(nhop) == "if1 1111:1111::"
nhop = next_hop.NextHop("if2", None)
assert str(nhop) == "if2"
nhop = next_hop.NextHop(None, None)
assert str(nhop) == ""
示例15: __init__
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import IPv6Address [as 别名]
def __init__(self, interface, address):
assert (interface is None) or isinstance(interface, str)
assert ((address is None) or
isinstance(address, (ipaddress.IPv4Address, ipaddress.IPv6Address)))
self.interface = interface
self.address = address