Last active
February 24, 2016 20:19
-
-
Save coxley/9165ac5316195d4a5a29 to your computer and use it in GitHub Desktop.
ASPLAIN to ASDOT (AS-PLAIN AS-DOT) and reverse
This file contains hidden or 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
def plain_to_dot(asn): # -> str | |
'''Take ASPLAIN and return ASDOT notation | |
asn: int | |
''' | |
barray = struct.pack('>I', asn) | |
return '%d.%d' % struct.unpack('>HH', barray) | |
def dot_to_plain(asn): # -> int | |
'''Take ASDOT and return ASPLAIN notation | |
asn: string - two nums separated by period (.) | |
''' | |
a1, a2 = asn.split('.') | |
barray = struct.pack('>HH', int(a1), int(a2)) | |
return struct.unpack('>I', barray)[0] | |
def bytesize(i): # -> int | |
'''Return bytesize''' | |
return (i.bit_length() + 7) // 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hold onto this for dropbox/nsot#148 :)