/ Published in: Python
Convert an IP address to an integer for easier storage. Also, convert an integer back to an IP address.
Expand |
Embed | Plain Text
def IntToDottedIP( intip ): octet = '' for exp in [3,2,1,0]: octet = octet + str(intip / ( 256 ** exp )) + "." intip = intip % ( 256 ** exp ) return(octet.rstrip('.')) def DottedIPToInt( dotted_ip ): exp = 3 intip = 0 for quad in dotted_ip.split('.'): intip = intip + (int(quad) * (256 ** exp)) exp = exp - 1 return(intip)
Comments
Subscribe to comments
You need to login to post a comment.

def test(): passConvert dotted quad notation to integer
st="127.0.0.1".split('.') int("%02x%02x%02x%02x" % (int(st[0]),int(st[1]),int(st[2]),int(st[3])),16) # = 2130706433