How to fix python ipaddress.IPv6Address AddressValueError: Unexpected '/' in '.../64'
Problem:
When trying to parse an IPv6 network address in Python using code like
import ipaddress
ipaddress.IPv6Network("2a01:c23:c0bb:d00:8ce6:2eff:fe60:cc69/64")
you see an error message like
---------------------------------------------------------------------------
AddressValueError Traceback (most recent call last)
/tmp/ipykernel_154945/2602627019.py in <module>
----> 1 ipaddress.IPv6Address("2a01:c23:c0bb:d00:8ce6:2eff:fe60:cc69/64")
/usr/lib/python3.8/ipaddress.py in __init__(self, address)
1836 addr_str = str(address)
1837 if '/' in addr_str:
-> 1838 raise AddressValueError("Unexpected '/' in %r" % address)
1839 self._ip = self._ip_int_from_string(addr_str)
1840
AddressValueError: Unexpected '/' in '2a01:c23:c0bb:d00:8ce6:2eff:fe60:cc69/64'
Solution 1: Remove the CIDR netmask (/64
)
By just removing the slash and the part after it (the CIDR netmask).
Solution 1: Maybe you should use IPv6Network
instead of IPv6Address
If you intend to parse the network, use ipaddress.IPv6Network
but remember that this will discard all host bits. If you want to use IPv6Address
or IPv6Network
really depends on what you want to do with it - if you want to refer to a specific host, you almost always want to use IPv6Address
.
import ipaddress
ipaddress.IPv6Network("2a01:c23:c0bb:d00:8ce6:2eff:fe60:cc69/64", strict=False)
Note that strict=False
is added in order to prevent an exception due to host bits being set - see How to fix Python ipaddress.IPv6Network ValueError: … has host bits set