Skip to content

Instantly share code, notes, and snippets.

@jbarham
Created May 29, 2012 23:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbarham/2831485 to your computer and use it in GitHub Desktop.
Save jbarham/2831485 to your computer and use it in GitHub Desktop.
Converts an IPv6 address to octal format for tinydns
# This function is a translation of the PHP function described at
# http://thisisnotajoke.com/2011/07/12/aaaa-for-tinydns-php-function/
def ipv6octal(ipv6):
while len(ipv6.split(':')) < 8:
ipv6 = ipv6.replace('::', ':::')
octets = []
for part in ipv6.split(':'):
if not part:
octets.extend([0, 0])
else:
# Pad hex part to 4 digits.
part = '%04x' % int(part, 16)
octets.append(int(part[:2], 16))
octets.append(int(part[2:], 16))
return '\\' + '\\'.join(['%03o' % x for x in octets])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment