break
return (first, last)
+
def _get_prefix_length(number1, number2, bits):
"""Get the number of leading bits that are same for two numbers.
return bits - i
return 0
+
def _count_righthand_zero_bits(number, bits):
"""Count the number of zero bits on the right hand side.
if (number >> i) % 2:
return i
+
def summarize_address_range(first, last):
"""Summarize a network range given the first and last IP addresses.
raise TypeError('first and last must be IP addresses, not networks')
if first.version != last.version:
raise TypeError("%s and %s are not of the same version" % (
- str(first), str(last)))
+ str(first), str(last)))
if first > last:
raise ValueError('last IP address must be greater than first')
first = IPAddress(first_int, version=first._version)
return networks
+
def _collapse_address_list_recursive(addresses):
"""Loops through the addresses, collapsing concurrent netblocks.
if isinstance(ip, _BaseIP):
if ips and ips[-1]._version != ip._version:
raise TypeError("%s and %s are not of the same version" % (
- str(ip), str(ips[-1])))
+ str(ip), str(ips[-1])))
ips.append(ip)
elif ip._prefixlen == ip._max_prefixlen:
if ips and ips[-1]._version != ip._version:
raise TypeError("%s and %s are not of the same version" % (
- str(ip), str(ips[-1])))
+ str(ip), str(ips[-1])))
ips.append(ip.ip)
else:
if nets and nets[-1]._version != ip._version:
raise TypeError("%s and %s are not of the same version" % (
- str(ip), str(ips[-1])))
+ str(ip), str(ips[-1])))
nets.append(ip)
# sort and dedup
def __repr__(self):
return 'Bytes(%s)' % str.__repr__(self)
+
def get_mixed_type_key(obj):
"""Return a key suitable for sorting between networks and addresses.
return obj._get_address_key()
return NotImplemented
+
class _IPAddrBase(object):
"""The mother class."""
def __lt__(self, other):
if self._version != other._version:
raise TypeError('%s and %s are not of the same version' % (
- str(self), str(other)))
+ str(self), str(other)))
if not isinstance(other, _BaseIP):
raise TypeError('%s and %s are not of the same type' % (
- str(self), str(other)))
+ str(self), str(other)))
if self._ip != other._ip:
return self._ip < other._ip
return False
def __gt__(self, other):
if self._version != other._version:
raise TypeError('%s and %s are not of the same version' % (
- str(self), str(other)))
+ str(self), str(other)))
if not isinstance(other, _BaseIP):
raise TypeError('%s and %s are not of the same type' % (
- str(self), str(other)))
+ str(self), str(other)))
if self._ip != other._ip:
return self._ip > other._ip
return False
return '%s(%r)' % (self.__class__.__name__, str(self))
def __str__(self):
- return '%s' % self._string_from_ip_int(self._ip)
+ return '%s' % self._string_from_ip_int(self._ip)
def __hash__(self):
return hash(hex(long(self._ip)))
def __lt__(self, other):
if self._version != other._version:
raise TypeError('%s and %s are not of the same version' % (
- str(self), str(other)))
+ str(self), str(other)))
if not isinstance(other, _BaseNet):
raise TypeError('%s and %s are not of the same type' % (
- str(self), str(other)))
+ str(self), str(other)))
if self.network != other.network:
return self.network < other.network
if self.netmask != other.netmask:
def __gt__(self, other):
if self._version != other._version:
raise TypeError('%s and %s are not of the same version' % (
- str(self), str(other)))
+ str(self), str(other)))
if not isinstance(other, _BaseNet):
raise TypeError('%s and %s are not of the same type' % (
- str(self), str(other)))
+ str(self), str(other)))
if self.network != other.network:
return self.network > other.network
if self.netmask != other.netmask:
return not eq
def __str__(self):
- return '%s/%s' % (str(self.ip),
- str(self._prefixlen))
+ return '%s/%s' % (str(self.ip),
+ str(self._prefixlen))
def __hash__(self):
return hash(int(self.network) ^ int(self.netmask))
def __contains__(self, other):
# always false if one is v4 and the other is v6.
if self._version != other._version:
- return False
+ return False
# dealing with another network.
if isinstance(other, _BaseNet):
return (self.network <= other.network and
# Make sure we're comparing the network of other.
other = IPNetwork('%s/%s' % (str(other.network), str(other.prefixlen)),
- version=other._version)
+ version=other._version)
s1, s2 = self.subnet()
while s1 != other and s2 != other:
s1, s2 = s2.subnet()
else:
# If we got here, there's a bug somewhere.
- assert True == False, ('Error performing exclusion: '
+ assert True is False, ('Error performing exclusion: '
's1: %s s2: %s other: %s' %
(str(s1), str(s2), str(other)))
if s1 == other:
ret_addrs.append(s1)
else:
# If we got here, there's a bug somewhere.
- assert True == False, ('Error performing exclusion: '
+ assert True is False, ('Error performing exclusion: '
's1: %s s2: %s other: %s' %
(str(s1), str(s2), str(other)))
first = IPNetwork('%s/%s' % (str(self.network),
str(self._prefixlen + prefixlen_diff)),
- version=self._version)
+ version=self._version)
yield first
current = first
raise ValueError('cannot set prefixlen_diff and new_prefix')
prefixlen_diff = self._prefixlen - new_prefix
-
if self.prefixlen - prefixlen_diff < 0:
raise ValueError(
'current prefixlen is %d, cannot have a prefixlen_diff of %d' %
@property
def is_reserved(self):
- """Test if the address is otherwise IETF reserved.
+ """Test if the address is otherwise IETF reserved.
Returns:
A boolean, True if the address is within the
reserved IPv4 Network range.
- """
- return self in IPv4Network('240.0.0.0/4')
+ """
+ return self in IPv4Network('240.0.0.0/4')
@property
def is_private(self):
# We have dotted decimal netmask.
if self._is_valid_netmask(addr[1]):
self.netmask = IPv4Address(self._ip_int_from_string(
- addr[1]))
+ addr[1]))
elif self._is_hostmask(addr[1]):
self.netmask = IPv4Address(
self._ip_int_from_string(addr[1]) ^ self._ALL_ONES)
else:
- raise NetmaskValueError('%s is not a valid netmask'
- % addr[1])
+ raise NetmaskValueError('%s is not a valid netmask' % addr[1])
self._prefixlen = self._prefix_from_ip_int(int(self.netmask))
else:
if len(mask) == 4:
if [x for x in mask if int(x) not in self._valid_mask_octets]:
return False
- if [y for idx, y in enumerate(mask) if idx > 0 and
- y > mask[idx - 1]]:
+ if [y for idx, y in enumerate(mask) if idx > 0 and y > mask[idx - 1]]:
return False
return True
try:
class IPv6Network(_BaseV6, _BaseNet):
-
"""This class represents and manipulates 128-bit IPv6 networks.
Attributes: [examples for IPv6('2001:658:22A:CAFE:200::1/64')]
"""
-
def __init__(self, address, strict=False):
"""Instantiate a new IPv6 Network object.