sorting on IP addresses

Ben Gertzfield che at debian.org
Mon Feb 5 17:25:06 EST 2001


>>>>> "Sam" == Sam Wun <swun at esec.com.au> writes:

    Sam> Hi, Does anyone know what is the quickly way to sort a list
    Sam> of IP addresses?

    Sam> ie. 203.21.254.89 should be larger than 203.21.254.9

If you're using Python 1.6 or greater, the socket module includes
inet_aton and inet_ntoa functions, used for converting IP addresses
from 123.45.67.89 format to the 4-byte "packed IP" representation.

You can then just use a normal sort on the packed IPs.

Ben

-- 
Brought to you by the letters P and R and the number 1.
"Atatakaku natta kara, otaku to uchi no kazoku de, nani ka shimasen ka?"
Debian GNU/Linux maintainer of Gimp and GTK+ -- http://www.debian.org/



More information about the Python-list mailing list