Package netaddr :: Module address :: Class IP
[frames] | no frames]

Class IP

source code

object --+    
         |    
      Addr --+
             |
            IP

A class whose objects represent Internet Protocol network addresses. Both IPv4 and IPv6 are fully supported and also permit the inclusion of bitmask prefix or subnet mask address indicating the size/extent of the subnet, for example :

   IPv4

   192.168.0.1/24
   192.168.0.1/255.255.255.0

   IPv6

   fe80::20f:1fff:fe12:e733/64
Instance Methods
 
__init__(self, addr, addr_type=0)
Constructor.
source code
 
is_netmask(self)
Returns: True if this addr is a mask that would return a host id, False otherwise.
source code
 
netmask_bits(self)
Returns: If this address is a valid netmask, the number of non-zero bits are returned, otherwise it returns the width (in bits) for the given address type (IPv4: 32, IPv6: 128).
source code
 
reverse_dns(self)
Returns: The reverse DNS lookup string for this IP address.
source code
 
is_hostmask(self)
Returns: True if this address is a mask that would return a host id, False otherwise.
source code
 
hostname(self)
Returns: Returns the FQDN for this IP address via a DNS query using gethostbyaddr() Python's socket module.
source code
 
cidr(self)
Returns: A valid CIDR object for this IP address.
source code
 
ipv4(self)
Returns: A new IP object numerically equivalent this address.
source code
 
ipv6(self)
Returns: A new IP object numerically equivalent this address.
source code
 
is_unicast(self)
Returns: True if this address is unicast, False otherwise.
source code
 
is_multicast(self)
Returns: True if this address is multicast, False otherwise.
source code
 
__str__(self)
Returns: The common string representation for this IP address.
source code
 
__repr__(self)
Returns: An executable Python statement that can recreate an object with an equivalent state.
source code

Inherited from Addr: __eq__, __ge__, __getitem__, __gt__, __hash__, __hex__, __iadd__, __int__, __isub__, __iter__, __le__, __len__, __long__, __lt__, __ne__, __setitem__, bits

Inherited from object: __delattr__, __getattribute__, __new__, __reduce__, __reduce_ex__, __setattr__

Class Variables
  STRATEGIES = (netaddr.address.IPv4StrategyOpt(32, 8, '.', 4, F...
  ADDR_TYPES = (0, 4, 6)
  TRANSLATE_STR = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\...
  strategy = StrategyDescriptor(STRATEGIES)
  addr_type = AddrTypeDescriptor(ADDR_TYPES)
  prefixlen = PrefixLenDescriptor()

Inherited from Addr: value

Properties

Inherited from object: __class__

Method Details

__init__(self, addr, addr_type=0)
(Constructor)

source code 

Constructor.

Parameters:
  • addr - an IPv4 or IPv6 address string with an optional subnet prefix or a network byte order integer.
  • addr_type - (optional) the IP address type (AT_INET or AT_INET6). If addr is an integer, this argument is mandatory.
Overrides: object.__init__

is_netmask(self)

source code 
Returns:
True if this addr is a mask that would return a host id, False otherwise.

netmask_bits(self)

source code 
Returns:
If this address is a valid netmask, the number of non-zero bits are returned, otherwise it returns the width (in bits) for the given address type (IPv4: 32, IPv6: 128).

reverse_dns(self)

source code 
Returns:
The reverse DNS lookup string for this IP address.

is_hostmask(self)

source code 
Returns:
True if this address is a mask that would return a host id, False otherwise.

hostname(self)

source code 
Returns:
Returns the FQDN for this IP address via a DNS query using gethostbyaddr() Python's socket module.

cidr(self)

source code 
Returns:
A valid CIDR object for this IP address.

ipv4(self)

source code 
Returns:
A new IP object numerically equivalent this address.
  • If its address type is IPv4.
  • If object's address type is IPv6 and its value is mappable to IPv4, a new IPv4 IP object is returned instead.
  • Raises an AddrConversionError if IPv6 address is not mappable to IPv4.

ipv6(self)

source code 
Returns:
A new IP object numerically equivalent this address.
  • If object's address type is IPv6.
  • If object's address type is IPv4 a new IPv6 IP object, as a IPv4 mapped address is returned instead. Uses the preferred IPv4 embedded in IPv6 form - ::ffff:x.x.x.x ('mapped' address) over the (now deprecated) form - ::x.x.x.x ('compatible' address). See RFC 4921 for details.

is_unicast(self)

source code 
Returns:
True if this address is unicast, False otherwise.

is_multicast(self)

source code 
Returns:
True if this address is multicast, False otherwise.

__str__(self)
(Informal representation operator)

source code 

str(x)

Returns:
The common string representation for this IP address.
Overrides: object.__str__

__repr__(self)
(Representation operator)

source code 

repr(x)

Returns:
An executable Python statement that can recreate an object with an equivalent state.
Overrides: object.__repr__

Class Variable Details

STRATEGIES

Value:
(netaddr.address.IPv4StrategyOpt(32, 8, '.', 4, False, False),
 netaddr.address.IPv6Strategy(128, 16, ':', 6, True, False))

TRANSLATE_STR

Value:
'''\x00\x01\x02\x03\x04\x05\x06\x07\x08\t
\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\
\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX\
YZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x8\
6\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\\
x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa\
9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\\
xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xc\
...