This is the code I used for this purpose before. Note that it returns an integer of 128 bits, not a string (the integer is more useful in general)
from socket import inet_pton, AF_INET6
from struct import unpack
def ip6_to_integer(ip6):
ip6 = inet_pton(AF_INET6, ip6)
a, b = unpack(">QQ", ip6)
return (a << 64) | b
And testing him
>>> ip6_to_integer("2001:23::207:142")
42540490934961530759802172199372521794L
Or as a string, if necessary!
>>> str(ip6_to_integer("2001:23::207:142"))
'42540490934961530759802172199372521794'
source
share