24 lines
636 B
Python
Raw Normal View History

"""
Misc. IP tools: normalize, handle mapped addresses
"""
from netaddr import IPAddress
def normalize_mapped_address(ipaddr):
2014-09-29 11:31:22 +02:00
"""
Converts a IPv4-mapped IPv6 address into a IPv4 address. Handles both the
::ffff:192.0.2.128 format as well as the deprecated ::192.0.2.128 format.
2014-09-29 11:31:22 +02:00
:param ipaddr: IP address [str]
:return: normalized IP address [str]
"""
ipaddr = IPAddress(ipaddr)
if ipaddr.is_ipv4_compat() or ipaddr.is_ipv4_mapped():
ipaddr = ipaddr.ipv4()
return str(ipaddr)
2014-09-29 11:31:22 +02:00
# currently, normalize_ip does no more than normalize_mapped_address:
normalize_ip = normalize_mapped_address