I'm trying to get the local IPV6 address by reading /proc/net/if_inet6
. But the address there is without any colons like: 000000000000000000000abc00070def
.
So I'm adding colons (:) at multiple places like this:
str6 = "000000000000000000000abc00070def"
i = 4
addr = str6[:i]
while i < len(str6):
addr += ":" + str6[i:i+4]
i += 4
print addr # output: 0000:0000:0000:0000:0000:0abc:0007:0def
For a normal string this is working fine.
But since the string is a IPV6 address, so wondering is there a better way to do it? (Tried with ipaddress module but I don't think it supports without colons.)
ipaddress.IPv6Address
can also construct an IPv6 address from a "valid" integer:
import ipaddress
ipv6_addr = ipaddress.ip_address(int('000000000000000000000abc00070def', 16))
print(ipv6_addr)
prints:
::abc:7:def
The long form of the address representation (exploded):
print(ipv6_addr.exploded)
prints:
0000:0000:0000:0000:0000:0abc:0007:0def