[Python-Dev] Issues with Py3.1's new ipaddr

Paul Moore p.f.moore at gmail.com
Wed Jun 3 10:02:25 CEST 2009


2009/6/2 R. David Murray <rdmurray at bitdance.com>:
> On Tue, 2 Jun 2009 at 21:02, Paul Moore wrote:
>> Simple example. If I want to scan all the IP addresses on my network
>> (my IP address is 192.168.1.101) I'd probably write:
>>
>>   for i in range(253):
>>       ip = '192.168.1.' + str(i+1)
>>       ...
>>
>> - and to heck with generality.
>>
>> Even after reading the documentation, I've *no idea* how I would use
>> the ipaddr module to do this better. Let alone in as few lines.
>
>    net = ipaddr.IP('192.168.1.0/24'):
>    for i in range(253):
>        ip = net[i]
>        ...
>
> So, that's one example that needs to be added to the docs.
>
> I'd have liked to write that as:
>
>    for ip in ipaddr.IP('192.168.1.0/24')[:253]:
>        ...
>
> but apparently it doesn't support slicing (time for an RFE :)

Given that what I *want* to do is to skip the "special" cases of 0 and
255, would the following work?

net = ipaddr.IP('192.168.1.101/255.255.255.0') # Note 1
for ip in net:
    if ip.ip = ip.broadcast or ip.is_multicast():
        continue
    ...

That would be what I mean by the module helping me to avoid "gotchas"
- like assuming 0 and 255 are the "special" multicast and broadcast
(or whatever they are) addresses that I shouldn't be testing in my
port scanner.

I'd like a .is_broadcast() method. Does that expose my lack of
understanding, or is it a sensible thing to add?

Note 1 - by the way, I use this form because I don't understand how
the /24 notation works. I can get the subnet mask from ipconfig, so I
use that. Ideally, I'd rather just put my IP address and have the
module work out the "obvious" subnet (at my level of use, it's always
255.255.255.0 for 192.168 addresses) but I guess that's not actually a
well-defined idea.

Paul.


More information about the Python-Dev mailing list