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

# Class IP

source code

    object --+
             |
          Addr --+
                 |
                IP

Represents individual IPv4 and IPv6 addresses.

Please Note: this class is intended to provide low-level functionality to individual IP addresses such as octet/hextet access, integer/hex/binary conversions, etc. If you are coming from other libraries you may expect to find much higher level networking operations here. While the inclusion of a bitmask prefix or netmask to indicate subnet membership is permitted by the class constructor they are provided only as a convenience to the user.

All higher level subnet and network operations can be found in objects of classes CIDR, IPRange and Wildcard. There are handy helper methods here, (`.cidr()`, `.iprange()` and `.wildcard()`) that return pre-initialised objects of those classes without you having to call them explicitly.

Example usage :

       >>> ip = IP('10.0.0.1')
       >>> list(ip) == [10, 0, 0, 1]
       True
       >>> ip += 1
       >>> str(ip) == '10.0.0.2'
       True

       >>> IP('10.0.0.0/28').iprange()
       IPRange('10.0.0.0', '10.0.0.15')

       >>> IP('10.0.0.64/24').cidr()
       CIDR('10.0.0.0/24')

       >>> IP('192.168.0.1/255.255.253.0').wildcard()
       Wildcard('192.168.0-1.*')

       >>> ipv6 = IP('fe80::20f:1fff:fe12:e733')
       >>> ipv6[0:4]
       [65152, 0, 0, 0]

       >>> IP('fe80::20f:1fff:fe12:e733/64').cidr()
       CIDR('fe80::/64')

See those classes for details on the functionality they provide.

Instance Methods | [hide private]
__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 based on the version, 32 for IPv4 and 128 for IPv6. | 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, strict=True) Returns: A CIDR object based on this IP address | source code
wildcard(self) Returns: A Wildcard object based on this IP address | source code
iprange(self) Returns: A CIDR object based on this IP address | source code
ipv4(self) Returns: A new version 4 IP object numerically equivalent this address. | source code
ipv6(self, ipv4_compatible=False) Please Note: the IPv4-Mapped IPv6 address format is now considered deprecated. | source code
is_unicast(self) Returns: `True` if this IP is unicast, `False` otherwise | source code
is_loopback(self) Returns: `True` if this IP is loopback address (not for network transmission), `False` otherwise. | source code
is_multicast(self) Returns: `True` if this IP is multicast, `False` otherwise | source code
is_private(self) Returns: `True` if this IP is for internal/private use only (i.e. | source code
is_link_local(self) Returns: `True` if this IP is link-local address `False` otherwise. | source code
is_reserved(self) Returns: `True` if this IP is in IANA reserved range, `False` otherwise. | source code
is_ipv4_mapped(self) Returns: `True` if this IP is IPv4-compatible IPv6 address, `False` otherwise. | source code
is_ipv4_compat(self) Returns: `True` if this IP is IPv4-mapped IPv6 address, `False` otherwise. | source code
info(self) Returns: A record dict containing IANA registration details for this IP address if available, None otherwise. | source code
__str__(self) Returns: common string representation for this IP address | source code
__repr__(self) Returns: executable Python string to recreate equivalent object. | source code
Inherited from `object`: `__delattr__`, `__getattribute__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__setattr__`
Inherited from Addr
__add__(self, other) Returns: A new (potentially larger) Addr class/subclass instance. | source code
__and__(self, other) Returns: bitwise AND (x & y) between the integer value of this address and another. | source code
__eq__(self, other) Returns: `True` if this address is numerically the same as other, `False` otherwise. | source code
__ge__(self, other) Returns: `True` if this address is numerically greater or equal in value to other, `False` otherwise. | source code
__getitem__(self, index) Returns: The integer value of the word referenced by index (both positive and negative). | source code
__gt__(self, other) Returns: `True` if this address is numerically greater in value than other, `False` otherwise. | source code
__hash__(self) Returns: hash of this address suitable for dict keys, sets etc | source code
__hex__(self) Returns: hexadecimal string representation of this address (in network byte order). | source code
__iadd__(self, num) Increment value of network address by specified amount. | source code
__int__(self) Returns: value of this address as an unsigned integer | source code
__isub__(self, num) Decrement value of network address by specified amount. | source code
__iter__(self) Returns: An iterator over individual words in this address | source code
__le__(self, other) Returns: `True` if this address is numerically lower or equal in value to other, `False` otherwise. | source code
__len__(self) Returns: The size (width) of this address in bits | source code
__long__(self) Returns: value of this address as an unsigned integer | source code
__lshift__(self, numbits) Returns: an address based on this one with its integer value left shifted by x bits. | source code
__lt__(self, other) Returns: `True` if this address is numerically lower in value than other, `False` otherwise. | source code
__ne__(self, other) Returns: `False` if this address is numerically the same as the other, `True` otherwise. | source code
__nonzero__(self) Returns: True if the numerical value of this address is not zero, False otherwise. | source code
__or__(self, other) Returns: bitwise OR (x | y) between the integer value of this IP address and another. | source code
__rshift__(self, numbits) Returns: an address based on this one with its integer value right shifted by x bits. | source code
__setitem__(self, index, value) Sets the value of the word referenced by index in this address | source code
__sub__(self, other) Returns: A new (potentially smaller) Addr class/subclass instance. | source code
__xor__(self, other) Returns: bitwise exclusive OR (x ^ y) between the integer value of this address and another. | source code
bin(self) Returns: standard Python binary representation of this address. | source code
bits(self, word_sep=None) Returns: human-readable binary digit string of this address | source code
packed(self) Returns: binary packed string of this address | source code

Class Variables | [hide private]
| STRATEGIES = ``(`IPv4Strategy(32, 8, '.', 4, 10)`,` IPv6Strategy(12`...``
| 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()`
| _ = `255`
Inherited from Addr
| value = `AddrValueDescriptor('value')`

Properties | [hide private]
Inherited from `object`: `__class__`

Method Details | [hide private]

### __init__(self, addr, addr_type=0) (Constructor) | source code
Constructor. Parameters: `addr` - an IPv4 or IPv6 address string with an optional subnet prefix or an unsigned integer. `addr_type` - (optional) the IP address type (`AT_INET` or `AT_INET6`). This argument is used mainly to differentiate IPv4 and IPv6 addresses that may be numerically equivalent. 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 based on the version, 32 for IPv4 and 128 for IPv6.

### 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, strict=True) | source code
Parameters: `strict` - (optional) If True and non-zero bits are found to the right of the subnet mask/prefix a ValueError is raised. If False, CIDR returned has these bits automatically truncated. (default: True) Returns: A CIDR object based on this IP address

### wildcard(self) | source code
Returns: A Wildcard object based on this IP address

### iprange(self) | source code
Returns: A CIDR object based on this IP address

### ipv4(self) | source code
Returns: A new version 4 IP object numerically equivalent this address. If this object is already IPv4 then a copy is returned. If this object is IPv6 and its value is compatible with IPv4, a new IPv4 IP object is returned. Raises an AddrConversionError is IPv6 address cannot be converted.

### ipv6(self, ipv4_compatible=False) | source code
Please Note: the IPv4-Mapped IPv6 address format is now considered deprecated. Reference: RFC 4291 Parameters: `ipv4_compatible` - If `True` returns an IPv4-Mapped address (::ffff:x.x.x.x), an IPv4-Compatible (::x.x.x.x) address otherwise. Default: False (IPv4-Mapped). Returns: A new IP version 6 object that is numerically equivalent this address. If this object is already IPv6 then a copy of this object is returned. If this object is IPv4, a new version 6 IP object is returned.

### is_unicast(self) | source code
Returns: `True` if this IP is unicast, `False` otherwise

### is_loopback(self) | source code
Returns: `True` if this IP is loopback address (not for network transmission), `False` otherwise. References: RFC 3330 and 4291.

### is_multicast(self) | source code
Returns: `True` if this IP is multicast, `False` otherwise

### is_private(self) | source code
Returns: `True` if this IP is for internal/private use only (i.e. non-public), `False` otherwise. Reference: RFCs 1918, 3330, 4193, 3879 and 2365.

### is_link_local(self) | source code
Returns: `True` if this IP is link-local address `False` otherwise. Reference: RFCs 3927 and 4291.

### is_reserved(self) | source code
Returns: `True` if this IP is in IANA reserved range, `False` otherwise. Reference: RFCs 3330 and 3171.

### is_ipv4_mapped(self) | source code
Returns: `True` if this IP is IPv4-compatible IPv6 address, `False` otherwise.

### is_ipv4_compat(self) | source code
Returns: `True` if this IP is IPv4-mapped IPv6 address, `False` otherwise.

### info(self) | source code
Returns: A record dict containing IANA registration details for this IP address if available, None otherwise.

### __str__(self) (Informal representation operator) | source code
str(x) Returns: common string representation for this IP address Overrides: object.__str__

### __repr__(self) (Representation operator) | source code
repr(x) Returns: executable Python string to recreate equivalent object. Overrides: object.__repr__

Class Variable Details | [hide private]

  ### STRATEGIES Value:
      (IPv4Strategy(32, 8, '.', 4, 10), IPv6Strategy(128, 16, ':', 6, 16))

  ### 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
      ...