Created
May 29, 2012 23:40
-
-
Save jbarham/2831485 to your computer and use it in GitHub Desktop.
Converts an IPv6 address to octal format for tinydns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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