IP address and its netmask

Doug Stanfield DOUGS at oceanic.com
Wed Feb 7 09:15:15 EST 2001


I found this:

ftp://ftp.cendio.se/pub/playground/python/ipaddr-1.1.tar.gz

By way of the Vaults:

http://www.vex.net/parnassus/

Its a module that has a lot of very useful utility if you're working with IP
numbers and networks.  For example, your problem (changed slightly to make
it a challenge):

[dougs at lawehana dougs]$ python
Python 1.5.2 (#1, Apr 18 1999, 16:03:16)  [GCC pgcc-2.91.60 19981201
(egcs-1.1.1  on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import ipaddr
>>> aNet = ipaddr.network('203.21.67.0','255.255.252.0',ipaddr.DEMAND_NONE)
>>> aNet
{network 203.21.67.0/22}
>>> anIP = ipaddr.ipaddr('203.21.67.89')
>>> anIP
ipaddr.ipaddr('203.21.67.89')
>>> aNet.overlaps(anIP)
1
>>> otherIP = ipaddr.ipaddr('204.21.67.89')
>>> aNet.overlaps(otherIP)
0
>>>

-Doug-

> -----Original Message-----
> From: Sam Wun [mailto:swun at esec.com.au]
> Sent: Tuesday, February 06, 2001 7:26 PM
> To: python-list at python.org
> Subject: IP address and its netmask
> 
> 
> Hi,
> 
> I want to know whether 203.21.67.89 is in 203.21.67.0/32 

A /32 is a host address.  Maybe you meant something like /22?
> network. How do
> I do that in Python?
> 
> Thanks
> sam
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list