Package PyFoam :: Package ThirdParty :: Module IPy :: Class IPint
[show private | hide private]
[frames | no frames]

Class IPint

Known Subclasses:
IP

Handling of IP addresses returning integers.

Use class IP instead because some features are not implemented for IPint.
Method Summary
  __init__(self, data, ipversion)
Create an instance of an IP object.
  __cmp__(self, other)
Called by comparison operations.
  __contains__(self, item)
Called to implement membership test operators.
  __getitem__(self, key)
Called to implement evaluation of self[key].
  __hash__(self)
Called for the key object for dictionary operations, and by the built-in function hash() Should return a 32-bit integer usable as a hash value for dictionary operations.
  __len__(self)
Return the length of an subnet.
  __nonzero__(self)
All IPy objects should evaluate to true in boolean context.
  __repr__(self)
Print a representation of the Object.
  __str__(self)
Dispatch to the prefered String Representation.
  broadcast(self)
Return the broadcast (last) address of a network as an (long) integer.
  int(self)
Return the first / base / network addess as an (long) integer.
  iptype(self)
Return a description of the IP type ('PRIVATE', 'RESERVERD', etc).
  len(self)
Return the length of an subnet.
  net(self)
Return the base (first) address of a network as an (long) integer.
  netmask(self)
Return netmask as an integer.
  overlaps(self, item)
Check if two IP address ranges overlap.
  prefixlen(self)
Returns Network Prefixlen.
  strBin(self, wantprefixlen)
Return a string representation as a binary value.
  strCompressed(self, wantprefixlen)
Return a string representation in compressed format using '::' Notation.
  strDec(self, wantprefixlen)
Return a string representation in decimal format.
  strFullsize(self, wantprefixlen)
Return a string representation in the non mangled format.
  strHex(self, wantprefixlen)
Return a string representation in hex format in lower case.
  strNetmask(self)
Return netmask as an string.
  strNormal(self, wantprefixlen)
Return a string representation in the usual format.
  version(self)
Return the IP version of this Object.

Method Details

__init__(self, data, ipversion=0)
(Constructor)

Create an instance of an IP object.

Data can be a network specification or a single IP. IP Addresses can be specified in all forms understood by parseAddress.() the size of a network can be specified as

/prefixlen a.b.c.0/24 2001:658:22a:cafe::/64 -lastIP a.b.c.0-a.b.c.255 2001:658:22a:cafe::-2001:658:22a:cafe:ffff:ffff:ffff:ffff /decimal netmask a.b.c.d/255.255.255.0 not supported for IPv6

If no size specification is given a size of 1 address (/32 for IPv4 and /128 for IPv6) is assumed.
>>> 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
See module documentation for more examples.

__cmp__(self, other)
(Comparison operator)

Called by comparison operations.

Should return a negative integer if self < other, zero if self == other, a positive integer if self > other.

Networks with different prefixlen are considered non-equal. Networks with the same prefixlen and differing addresses are considered non equal but are compared by thair base address integer value to aid sorting of IP objects.

The Version of Objects is not put into consideration.
>>> IP('10.0.0.0/24') > IP('10.0.0.0')
1

>>> IP('10.0.0.0/24') < IP('10.0.0.0')
0

>>> IP('10.0.0.0/24') < IP('12.0.0.0/24')
1

>>> IP('10.0.0.0/24') > IP('12.0.0.0/24')
0

__contains__(self, item)
(In operator)

Called to implement membership test operators.

Should return true if item is in self, false otherwise. Item can be other IP-objects, strings or ints.
>>> IP('195.185.1.1').strHex()
'0xc3b90101'

>>> 0xC3B90101L in IP('195.185.1.0/24')
1

>>> '127.0.0.1' in IP('127.0.0.0/24')
1

>>> IP('127.0.0.0/24') in IP('127.0.0.0/25')
0

__getitem__(self, key)
(Indexing operator)

Called to implement evaluation of self[key].
>>> ip=IP('127.0.0.0/30')
>>> for x in ip:
...  print repr(x)
...

IP('127.0.0.0')
IP('127.0.0.1')
IP('127.0.0.2')
IP('127.0.0.3')

>>> ip[2]
IP('127.0.0.2')

>>> ip[-1]
IP('127.0.0.3')

__hash__(self)
(Hashing function)

Called for the key object for dictionary operations, and by the built-in function hash() Should return a 32-bit integer usable as a hash value for dictionary operations. The only required property is that objects which compare equal have the same hash value
>>> IP('10.0.0.0/24').__hash__()
-167772185

__len__(self)
(Length operator)

Return the length of an subnet.

Called to implement the built-in function len(). It breaks with IPv6 Networks. Anybody knows how to fix this.

__nonzero__(self)
(Boolean test operator)

All IPy objects should evaluate to true in boolean context. Ordinarily, they do, but if handling a default route expressed as 0.0.0.0/0, the __len__() of the object becomes 0, which is used as the boolean value of the object.

__repr__(self)
(Representation operator)

Print a representation of the Object.

Used to implement repr(IP). Returns a string which evaluates to an identical Object (without the wnatprefixlen stuff - see module docstring.
>>> print repr(IP('10.0.0.0/24'))
IP('10.0.0.0/24')

__str__(self)
(Informal representation operator)

Dispatch to the prefered String Representation.

Used to implement str(IP).

broadcast(self)

Return the broadcast (last) address of a network as an (long) integer.

The same as IP[-1].

int(self)

Return the first / base / network addess as an (long) integer.

The same as IP[0].
>>> "%X" % IP('10.0.0.0/8').int()
'A000000'

iptype(self)

Return a description of the IP type ('PRIVATE', 'RESERVERD', etc).
>>> print IP('127.0.0.1').iptype()
PRIVATE

>>> print IP('192.168.1.1').iptype()
PRIVATE

>>> print IP('195.185.1.2').iptype()
PUBLIC

>>> print IP('::1').iptype()
LOOPBACK

>>> print IP('2001:0658:022a:cafe:0200::1').iptype()
ASSIGNABLE RIPE
The type information for IPv6 is out of sync with reality.

len(self)

Return the length of an subnet.
>>> print IP('195.185.1.0/28').len()
16

>>> print IP('195.185.1.0/24').len()
256

net(self)

Return the base (first) address of a network as an (long) integer.

netmask(self)

Return netmask as an integer.
>>> "%X" % IP('195.185.0.0/16').netmask().int()
'FFFF0000'

overlaps(self, item)

Check if two IP address ranges overlap.

Returns 0 if the two ranged don't overlap, 1 if the given range overlaps at the end and -1 if it does at the beginning.
>>> IP('192.168.0.0/23').overlaps('192.168.1.0/24')
1

>>> IP('192.168.0.0/23').overlaps('192.168.1.255')
1

>>> IP('192.168.0.0/23').overlaps('192.168.2.0')
0

>>> IP('192.168.1.0/24').overlaps('192.168.0.0/23')
-1

prefixlen(self)

Returns Network Prefixlen.
>>> IP('10.0.0.0/8').prefixlen()
8

strBin(self, wantprefixlen=None)

Return a string representation as a binary value.
>>> print IP('127.0.0.1').strBin()
01111111000000000000000000000001

strCompressed(self, wantprefixlen=None)

Return a string representation in compressed format using '::' Notation.
>>> IP('127.0.0.1').strCompressed()
'127.0.0.1'

>>> IP('2001:0658:022a:cafe:0200::1').strCompressed()
'2001:658:22a:cafe:200::1'

>>> IP('ffff:ffff:ffff:ffff:ffff:f:f:fffc/127').strCompressed()
'ffff:ffff:ffff:ffff:ffff:f:f:fffc/127'

strDec(self, wantprefixlen=None)

Return a string representation in decimal format.
>>> print IP('127.0.0.1').strDec()
2130706433

>>> print IP('2001:0658:022a:cafe:0200::1').strDec()
42540616829182469433547762482097946625

strFullsize(self, wantprefixlen=None)

Return a string representation in the non mangled format.
>>> print IP('127.0.0.1').strFullsize()
127.0.0.1

>>> print IP('2001:0658:022a:cafe:0200::1').strFullsize()
2001:0658:022a:cafe:0200:0000:0000:0001

strHex(self, wantprefixlen=None)

Return a string representation in hex format in lower case.
>>> IP('127.0.0.1').strHex()
'0x7f000001'

>>> IP('2001:0658:022a:cafe:0200::1').strHex()
'0x20010658022acafe0200000000000001'

strNetmask(self)

Return netmask as an string. Mostly useful for IPv6.
>>> print IP('195.185.0.0/16').strNetmask()
255.255.0.0

>>> print IP('2001:0658:022a:cafe::0/64').strNetmask()
/64

strNormal(self, wantprefixlen=None)

Return a string representation in the usual format.
>>> print IP('127.0.0.1').strNormal()
127.0.0.1

>>> print IP('2001:0658:022a:cafe:0200::1').strNormal()
2001:658:22a:cafe:200:0:0:1

version(self)

Return the IP version of this Object.
>>> IP('10.0.0.0/8').version()
4

>>> IP('::1').version()
6

Generated by Epydoc 2.1 on Mon Jan 22 23:52:48 2007 http://epydoc.sf.net