views:

234

answers:

3

I have been handed the task of creating a function in python (3.1) that will take a CIDR notation and return the list of possible ip addresses. I have looked around python.org and found this: http://docs.python.org/dev/py3k/library/ipaddr.html

but i haven't seen anything that will fill this need... I would be very grateful for any assistance anyone cares to kick my way. thanks in advance. :-)

+1  A: 

Have you checked out iptools? It seems to be a fairly good fit.

Hank Gay
Given that it seems to not support Python 2.3, it would probably require at least a nominal amount of effort to support Python 3.1.
ephemient
I've updated iptools to support Python 2.3, 2.5, 2.6 and 3.1.
bd808
A: 

It's not in the documentation, but browsing the source suggests that ipaddr implements __iter__ and iterhosts, which is exactly what you want.


Err, nevermind.

  1. It looks like ipaddr.py was added to stdlib in 3.1 beta, but removed by 3.1 rc.
  2. I was looking at the sources from the original ipaddr.py, which seems to have evolved separately from the copy at python.org.

You could just bundle the latter.

ephemient
+2  A: 

If you aren't married to using the built-in module, there is a project called netaddr that is the best module I have used for working with IP networks.

Have a look at the IP Tutorial which illustrates how easy it is working with networks and discerning their IPs. Simple example:

>>> from netaddr import IPNetwork
>>> for ip in IPNetwork('192.0.2.0/23'):
...    print '%s' % ip
...
192.0.2.0
192.0.2.1
192.0.2.2
192.0.2.3
...
192.0.3.252
192.0.3.253
192.0.3.254
192.0.3.255
jathanism