Skip to main content

clean_ipv6_address

Clean an IPv6 address string.

Raise ValidationError if the address is invalid.

Replace the longest continuous zero-sequence with "::", remove leading zeroes, and make sure all hextets are lowercase.

def clean_ipv6_address(
ip_str: string,
unpack_ipv4: boolean = False,
error_message: string = _("This is not a valid IPv6 address."),
max_length: integer = MAX_IPV6_ADDRESS_LENGTH
) - > string

Clean an IPv6 address string. Replace the longest continuous zero-sequence with "::", remove leading zeroes, and make sure all hextets are lowercase. Raise ValidationError if the address is invalid.

Parameters

NameTypeDescription
ip_strstringThe raw IPv6 address string to be validated and formatted.
unpack_ipv4boolean = FalseDetermines whether to return a plain IPv4 address if an IPv4-mapped IPv6 address is encountered.
error_messagestring = _("This is not a valid IPv6 address.")The custom error message to be included in the ValidationError if the input string is not a valid IPv6 address.
max_lengthinteger = MAX_IPV6_ADDRESS_LENGTHThe maximum allowed character length for the input IP string before it is considered invalid.

Returns

TypeDescription
stringA compressed IPv6 address string, or a plain IPv4 address string if unpack_ipv4 is enabled and an IPv4-mapped address is detected.