Convert IP to Int and Int to IP


/ Published in: Python
Save to your folder(s)

Convert an IP address to an integer for easier storage.
Also, convert an integer back to an IP address.


Copy this code and paste it in your HTML
  1. def IntToDottedIP( intip ):
  2. octet = ''
  3. for exp in [3,2,1,0]:
  4. octet = octet + str(intip / ( 256 ** exp )) + "."
  5. intip = intip % ( 256 ** exp )
  6. return(octet.rstrip('.'))
  7.  
  8. def DottedIPToInt( dotted_ip ):
  9. exp = 3
  10. intip = 0
  11. for quad in dotted_ip.split('.'):
  12. intip = intip + (int(quad) * (256 ** exp))
  13. exp = exp - 1
  14. return(intip)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.