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

Class IPint

source code

Handling of IP addresses returning integers.

Use class IP instead because some features are not implemented for IPint.

Instance Methods [hide private]
 
__init__(self, data, ipversion=0)
Create an instance of an IP object.
source code
 
int(self)
Return the first / base / network addess as an (long) integer.
source code
 
version(self)
Return the IP version of this Object.
source code
 
prefixlen(self)
Returns Network Prefixlen.
source code
 
net(self)
Return the base (first) address of a network as an (long) integer.
source code
 
broadcast(self)
Return the broadcast (last) address of a network as an (long) integer.
source code
 
_printPrefix(self, want)
Prints Prefixlen/Netmask.
source code
 
strBin(self, wantprefixlen=None)
Return a string representation as a binary value.
source code
 
strCompressed(self, wantprefixlen=None)
Return a string representation in compressed format using '::' Notation.
source code
 
strNormal(self, wantprefixlen=None)
Return a string representation in the usual format.
source code
 
strFullsize(self, wantprefixlen=None)
Return a string representation in the non mangled format.
source code
 
strHex(self, wantprefixlen=None)
Return a string representation in hex format in lower case.
source code
 
strDec(self, wantprefixlen=None)
Return a string representation in decimal format.
source code
 
iptype(self)
Return a description of the IP type ('PRIVATE', 'RESERVERD', etc).
source code
 
netmask(self)
Return netmask as an integer.
source code
 
strNetmask(self)
Return netmask as an string.
source code
 
len(self)
Return the length of an subnet.
source code
 
__nonzero__(self)
All IPy objects should evaluate to true in boolean context.
source code
 
__len__(self)
Return the length of an subnet.
source code
 
__getitem__(self, key)
Called to implement evaluation of self[key].
source code
 
__contains__(self, item)
Called to implement membership test operators.
source code
 
overlaps(self, item)
Check if two IP address ranges overlap.
source code
 
__str__(self)
Dispatch to the prefered String Representation.
source code
 
__repr__(self)
Print a representation of the Object.
source code
 
__cmp__(self, other)
Called by comparison operations.
source code
 
__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.
source code
Method Details [hide private]

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

source code 

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.

int(self)

source code 

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

The same as IP[0].

>>> "%X" % IP('10.0.0.0/8').int()
'A000000'

version(self)

source code 

Return the IP version of this Object.

>>> IP('10.0.0.0/8').version()
4
>>> IP('::1').version()
6

prefixlen(self)

source code 

Returns Network Prefixlen.

>>> IP('10.0.0.0/8').prefixlen()
8

broadcast(self)

source code 

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

The same as IP[-1].

_printPrefix(self, want)

source code 

Prints Prefixlen/Netmask.

Not really. In fact it is our universal Netmask/Prefixlen printer. This is considered an internel function.

want == 0 / None don't return anything 1.2.3.0 want == 1 /prefix 1.2.3.0/24 want == 2 /netmask 1.2.3.0/255.255.255.0 want == 3 -lastip 1.2.3.0-1.2.3.255

strBin(self, wantprefixlen=None)

source code 

Return a string representation as a binary value.

>>> print IP('127.0.0.1').strBin()
01111111000000000000000000000001

strCompressed(self, wantprefixlen=None)

source code 

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'

strNormal(self, wantprefixlen=None)

source code 

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

strFullsize(self, wantprefixlen=None)

source code 

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)

source code 

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'

strDec(self, wantprefixlen=None)

source code 

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

iptype(self)

source code 

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.

netmask(self)

source code 

Return netmask as an integer.

>>> "%X" % IP('195.185.0.0/16').netmask().int()
'FFFF0000'

strNetmask(self)

source code 

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

len(self)

source code 

Return the length of an subnet.

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

__nonzero__(self)
(Boolean test operator)

source code 

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.

__len__(self)
(Length operator)

source code 

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.

__getitem__(self, key)
(Indexing operator)

source code 

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')

__contains__(self, item)
(In operator)

source code 

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

overlaps(self, item)

source code 

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

__str__(self)
(Informal representation operator)

source code 

Dispatch to the prefered String Representation.

Used to implement str(IP).

__repr__(self)
(Representation operator)

source code 

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')

__cmp__(self, other)
(Comparison operator)

source code 

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

__hash__(self)
(Hashing function)

source code 

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