Python 3.3+ipaddressfunction
ipaddress.v6_int_to_packed()
packing to bytes
Converts an integer into the 16 bytes of an IPv6 address in network (big-endian) order.
Syntax
ipaddress.v6_int_to_packed(address)
The result is always exactly 16 bytes, even for small numbers: the address ::1 is fifteen zero bytes and a one. Numbers outside 0..2**128-1 raise ValueError. IPv6Address(b) reads the bytes back; note that ip_address() picks IPv4 for 4-byte input and IPv6 for 16-byte input.
Examples
import ipaddress
b = ipaddress.v6_int_to_packed(1)
print(len(b), b[-1]) # → 16 1
print(ipaddress.IPv6Address(b)) # → ::1
ipaddress.v6_int_to_packed(2 ** 128) # → ValueError: Address negative or too large for IPv6
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy