Skip to content

Instantly share code, notes, and snippets.

@rday
Created May 9, 2012 12:43
Show Gist options
  • Save rday/2644245 to your computer and use it in GitHub Desktop.
Save rday/2644245 to your computer and use it in GitHub Desktop.
Quick IP addresses functions
import socket
import struct
def quadtolong(ip):
return struct.unpack('!L',socket.inet_aton(ip))[0]
def longtoquad(long):
return socket.inet_ntoa(struct.pack('!L', long))
def inc_ip(ip):
return longtoquad(quadtolong(ip)+1)
def dec_ip(ip):
return longtoquad(quadtolong(ip)-1)
def ipsort(ip_list):
long_list = sorted([quadtolong(x) for x in ip_list])
return [longtoquad(x) for x in long_list]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment