Package netaddr :: Module address :: Class IP |
[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
__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) 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 `Addr`: `__add__`, `__and__`, `__eq__`, `__ge__`, `__getitem__`, `__gt__`, `__hash__`, `__hex__`, `__iadd__`, `__int__`, `__invert__`, `__isub__`, `__iter__`, `__le__`, `__len__`, `__long__`, `__lshift__`, `__lt__`, `__ne__`, `__or__`, `__rshift__`, `__setitem__`, `__sub__`, `__xor__`, `bin`, `bits`, `packed` Inherited from `object`: `__delattr__`, `__getattribute__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__setattr__`

Class Variables
| STRATEGIES = ``(`IPv4StrategyOpt(32, 8, '.', 4, 10, False)`,` IPv6S`...``
| 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`

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 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) | source code
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

  ### STRATEGIES Value:
      (IPv4StrategyOpt(32, 8, '.', 4, 10, False),
       IPv6Strategy(128, 16, ':', 6, 16, 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
      ...