Created
May 9, 2012 12:43
-
-
Save rday/2644245 to your computer and use it in GitHub Desktop.
Quick IP addresses functions
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
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