| IPy | index /home/drt/c0re/c0de/IPy/IPy.py |
IPy - class and tools for handling of IPv4 and IPv6 Addresses and Networks.
$Id: IPy.py,v 1.5 2001/12/22 21:12:11 drt Exp $
The IP class allows a comfortable parsing and handling for most
notations in use for IPv4 and IPv6 Addresses and Networks. It was
greatly inspired bei RIPE's Perl module NET::IP's interface but
doesn't share the Implementation. It doesn't share non-CIDR netmasks,
so funky stuff lixe a netmask 0xffffff0f can't be done here.
>>> ip = IP('127.0.0.0/30')
>>> for x in ip:
... print x
...
127.0.0.0
127.0.0.1
127.0.0.2
127.0.0.3
>>> ip2 = IP('0x7f000000/30')
>>> ip == ip2
1
>>> ip.reverseNames()
['0.0.0.127.in-addr.arpa.', '1.0.0.127.in-addr.arpa.', '2.0.0.127.in-addr.arpa.', '3.0.0.127.in-addr.arpa.']
>>> ip.reverseName()
'0-3.0.0.127.in-addr.arpa.'
>>> ip.iptype()
'PRIVATE'
It can detect about a dozen different ways of expressing IP addresses
and networks, parse them and distinguish between IPv4 and IPv6 addresses.
>>> IP('10.0.0.0/8').version()
4
>>> IP('::1').version()
6
>>> print IP(0x7f000001)
127.0.0.1
>>> print IP('0x7f000001')
127.0.0.1
>>> print IP('127.0.0.1')
127.0.0.1
>>> print IP('10')
10.0.0.0
>>> print IP('1080:0:0:0:8:800:200C:417A')
1080:0000:0000:0000:0008:0800:200c:417a
>>> print IP('1080::8:800:200C:417A')
1080:0000:0000:0000:0008:0800:200c:417a
>>> print IP('::1')
0000:0000:0000:0000:0000:0000:0000:0001
>>> print IP('::13.1.68.3')
0000:0000:0000:0000:0000:0000:0d01:4403
>>> print IP('127.0.0.0/8')
127.0.0.0/8
>>> print IP('127.0.0.0/255.0.0.0')
127.0.0.0/8
>>> print IP('127.0.0.0-127.255.255.255')
127.0.0.0/8
Nearly all class methods which return a string have an optional
parameter 'wantprefixlen' which controlles if the prefixlen or netmask
is printed. Per default the prefilen is always shown if the net
contains more than one address.
wantprefixlen == 0 / None don't return anything 1.2.3.0
wantprefixlen == 1 /prefix 1.2.3.0/24
wantprefixlen == 2 /netmask 1.2.3.0/255.255.255.0
wantprefixlen == 3 -lastip 1.2.3.0-1.2.3.255
You can also change the defaults on an per-object basis by fiddeling with the class members
NoPrefixForSingleIp
WantPrefixLen
>>> IP('10.0.0.0/32').strNormal()
'10.0.0.0'
>>> IP('10.0.0.0/24').strNormal()
'10.0.0.0/24'
>>> IP('10.0.0.0/24').strNormal(0)
'10.0.0.0'
>>> IP('10.0.0.0/24').strNormal(1)
'10.0.0.0/24'
>>> IP('10.0.0.0/24').strNormal(2)
'10.0.0.0/255.255.255.0'
>>> IP('10.0.0.0/24').strNormal(3)
'10.0.0.0-10.0.0.255'
>>> ip = IP('10.0.0.0')
>>> print ip
10.0.0.0
>>> ip.NoPrefixForSingleIp = None
>>> print ip
10.0.0.0/32
>>> ip.WantPrefixLen = 3
>>> print ip
10.0.0.0-10.0.0.0
Further Information might be available at http://c0re.23.nu/c0de/IPy/
Hacked 2001 by drt@un.bewaff.net
TODO:
* support for base85 encoding
* support for output of IPv6 encoded IPv4 Addresses
* update address type tables
* first-last notation should be allowed for IPv6
* add IPv6 docstring examples
* check better vor negative parameters
* add addition / aggregation
* move size in bits into class variables to get rid of some "if self._ipversion ..."
* move reverse name stuff out of the classes and refactor it
* support for aggregation of more than two nets at once
* support for aggregation with "holes"
* support for finding common prefix
* '>>' and '<<' for prefix manipulation
* better comparison (__cmp__ and friends)
* support for checking if two networks overlap.
* always write hex values lowercase
* interpret 2001:1234:5678:1234/64 as 2001:1234:5678:1234::/64
* rename checkPrefix to checkPrefixOk
* add more documentation and doctests
* refactor
| Modules | ||||||
| ||||||
| Classes | ||||||||||||||||||
| ||||||||||||||||||
| Functions | ||
| ||
| Data | ||
| IPv4ranges = {'0': 'PUBLIC', '00000000': 'PRIVATE', '00001010': 'PRIVATE', '01111111': 'PRIVATE', '1': 'PUBLIC', '101011000001': 'PRIVATE', '1100000010101000': 'PRIVATE', '11011111': 'RESERVED', '111': 'RESERVED'} IPv6ranges = {'00000000': 'RESERVED', '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000': 'IPV4COMP', '000000000000000000000000000000000000000000000000...0000000000000000000000000000000000000000000000000': 'UNSPECIFIED', '000000000000000000000000000000000000000000000000...0000000000000000000000000000000000000000000000001': 'LOOPBACK', '000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111': 'IPV4MAP', '00000001': 'UNASSIGNED', '0000001': 'NSAP', '0000010': 'IPX', '0000011': 'UNASSIGNED', '00001': 'UNASSIGNED', ...} _BitTable = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', '4': '0100', '5': '0101', '6': '0110', '7': '0111', '8': '1000', '9': '1001', ...} __file__ = './IPy.pyc' __name__ = 'IPy' | ||