You could use IPy... http://svn.23.nu/svn/repos/IPy/trunk/IPy.py is one location for it...
I wonder where you get O(n) and O(n^2) from... CIDR blocks are all sequential.. All you need to store is the starting and ending address or length. Then any set operation only has to deal with 4 numbers, and should be literally a few lines of code with no loops. -- http://mail.python.org/mailman/listinfo/python-list