import struct
-__version__ = '2.1.11'
+__version__ = "2.1.11"
IPV4LENGTH = 32
except (AddressValueError, NetmaskValueError):
pass
- raise ValueError('%r does not appear to be an IPv4 or IPv6 address' %
- address)
+ raise ValueError("%r does not appear to be an IPv4 or IPv6 address" % address)
def IPNetwork(address, version=None, strict=False):
except (AddressValueError, NetmaskValueError):
pass
- raise ValueError('%r does not appear to be an IPv4 or IPv6 network' %
- address)
+ raise ValueError("%r does not appear to be an IPv4 or IPv6 network" % address)
def v4_int_to_packed(address):
address.
"""
if address > _BaseV4._ALL_ONES:
- raise ValueError('Address too large for IPv4')
- return Bytes(struct.pack('!I', address))
+ raise ValueError("Address too large for IPv4")
+ return Bytes(struct.pack("!I", address))
def v6_int_to_packed(address):
Returns:
The binary representation of this address.
"""
- return Bytes(struct.pack('!QQ', address >> 64, address & (2**64 - 1)))
+ return Bytes(struct.pack("!QQ", address >> 64, address & (2**64 - 1)))
def _find_address_range(addresses):
"""
if not (isinstance(first, _BaseIP) and isinstance(last, _BaseIP)):
- raise TypeError('first and last must be IP addresses, not networks')
+ 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)))
+ raise TypeError(
+ "%s and %s are not of the same version" % (str(first), str(last))
+ )
if first > last:
- raise ValueError('last IP address must be greater than first')
+ raise ValueError("last IP address must be greater than first")
networks = []
elif first.version == 6:
ip = IPv6Network
else:
- raise ValueError('unknown IP version')
+ raise ValueError("unknown IP version")
ip_bits = first._max_prefixlen
first_int = first._ip
if current <= last_int:
break
prefix = _get_prefix_length(first_int, current, ip_bits)
- net = ip('%s/%d' % (str(first), prefix))
+ net = ip("%s/%d" % (str(first), prefix))
networks.append(net)
if current == ip._ALL_ONES:
break
for ip in addresses:
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])))
+ raise TypeError(
+ "%s and %s are not of the same version" % (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])))
+ raise TypeError(
+ "%s and %s are not of the same version" % (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(nets[-1])))
+ raise TypeError(
+ "%s and %s are not of the same version" % (str(ip), str(nets[-1]))
+ )
nets.append(ip)
# sort and dedup
i = ips.index(last) + 1
addrs.extend(summarize_address_range(first, last))
- return _collapse_address_list_recursive(sorted(
- addrs + nets, key=_BaseNet._get_networks_key))
+ return _collapse_address_list_recursive(
+ sorted(addrs + nets, key=_BaseNet._get_networks_key)
+ )
# backwards compatibility
raise TypeError("bytes is not a distinct type")
Bytes = bytes
except (NameError, TypeError):
+
class Bytes(str):
def __repr__(self):
- return 'Bytes(%s)' % str.__repr__(self)
+ return "Bytes(%s)" % str.__repr__(self)
def get_mixed_type_key(obj):
def __eq__(self, other):
try:
- return (self._ip == other._ip and
- self._version == other._version)
+ return self._ip == other._ip and self._version == other._version
except AttributeError:
return NotImplemented
def __lt__(self, other):
if self._version != other._version:
- raise TypeError('%s and %s are not of the same version' % (
- str(self), str(other)))
+ raise TypeError(
+ "%s and %s are not of the same version" % (str(self), str(other))
+ )
if not isinstance(other, _BaseIP):
- raise TypeError('%s and %s are not of the same type' % (
- str(self), str(other)))
+ raise TypeError(
+ "%s and %s are not of the same type" % (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)))
+ raise TypeError(
+ "%s and %s are not of the same version" % (str(self), str(other))
+ )
if not isinstance(other, _BaseIP):
- raise TypeError('%s and %s are not of the same type' % (
- str(self), str(other)))
+ raise TypeError(
+ "%s and %s are not of the same type" % (str(self), str(other))
+ )
if self._ip != other._ip:
return self._ip > other._ip
return False
return IPAddress(int(self) - other, version=self._version)
def __repr__(self):
- return '%s(%r)' % (self.__class__.__name__, str(self))
+ 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)))
+ return hash(hex(int(self._ip)))
def _get_address_key(self):
return (self._version, self)
@property
def version(self):
- raise NotImplementedError('BaseIP has no version')
+ raise NotImplementedError("BaseIP has no version")
class _BaseNet(_IPAddrBase):
self._cache = {}
def __repr__(self):
- return '%s(%r)' % (self.__class__.__name__, str(self))
+ return "%s(%r)" % (self.__class__.__name__, str(self))
def iterhosts(self):
"""Generate Iterator over usable hosts in a network.
- This is like __iter__ except it doesn't return the network
- or broadcast addresses.
+ This is like __iter__ except it doesn't return the network
+ or broadcast addresses.
"""
cur = int(self.network) + 1
def __lt__(self, other):
if self._version != other._version:
- raise TypeError('%s and %s are not of the same version' % (
- str(self), str(other)))
+ raise TypeError(
+ "%s and %s are not of the same version" % (str(self), str(other))
+ )
if not isinstance(other, _BaseNet):
- raise TypeError('%s and %s are not of the same type' % (
- str(self), str(other)))
+ raise TypeError(
+ "%s and %s are not of the same type" % (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)))
+ raise TypeError(
+ "%s and %s are not of the same version" % (str(self), str(other))
+ )
if not isinstance(other, _BaseNet):
- raise TypeError('%s and %s are not of the same type' % (
- str(self), str(other)))
+ raise TypeError(
+ "%s and %s are not of the same type" % (str(self), str(other))
+ )
if self.network != other.network:
return self.network > other.network
if self.netmask != other.netmask:
def __eq__(self, other):
try:
- return (self._version == other._version and
- self.network == other.network and
- int(self.netmask) == int(other.netmask))
+ return (
+ self._version == other._version
+ and self.network == other.network
+ and int(self.netmask) == int(other.netmask)
+ )
except AttributeError:
if isinstance(other, _BaseIP):
- return (self._version == other._version and
- self._ip == other._ip)
+ return self._version == other._version and self._ip == other._ip
def __ne__(self, other):
eq = self.__eq__(other)
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))
return False
# dealing with another network.
if isinstance(other, _BaseNet):
- return (self.network <= other.network and
- self.broadcast >= other.broadcast)
+ return self.network <= other.network and self.broadcast >= other.broadcast
# dealing with another address
else:
- return (int(self.network) <= int(other._ip) <=
- int(self.broadcast))
+ return int(self.network) <= int(other._ip) <= int(self.broadcast)
def overlaps(self, other):
"""Tell if self is partly contained in other."""
- return self.network in other or self.broadcast in other or (
- other.network in self or other.broadcast in self)
+ return (
+ self.network in other
+ or self.broadcast in other
+ or (other.network in self or other.broadcast in self)
+ )
@property
def network(self):
- x = self._cache.get('network')
+ x = self._cache.get("network")
if x is None:
x = IPAddress(self._ip & int(self.netmask), version=self._version)
- self._cache['network'] = x
+ self._cache["network"] = x
return x
@property
def broadcast(self):
- x = self._cache.get('broadcast')
+ x = self._cache.get("broadcast")
if x is None:
x = IPAddress(self._ip | int(self.hostmask), version=self._version)
- self._cache['broadcast'] = x
+ self._cache["broadcast"] = x
return x
@property
def hostmask(self):
- x = self._cache.get('hostmask')
+ x = self._cache.get("hostmask")
if x is None:
- x = IPAddress(int(self.netmask) ^ self._ALL_ONES,
- version=self._version)
- self._cache['hostmask'] = x
+ x = IPAddress(int(self.netmask) ^ self._ALL_ONES, version=self._version)
+ self._cache["hostmask"] = x
return x
@property
def with_prefixlen(self):
- return '%s/%d' % (str(self.ip), self._prefixlen)
+ return "%s/%d" % (str(self.ip), self._prefixlen)
@property
def with_netmask(self):
- return '%s/%s' % (str(self.ip), str(self.netmask))
+ return "%s/%s" % (str(self.ip), str(self.netmask))
@property
def with_hostmask(self):
- return '%s/%s' % (str(self.ip), str(self.hostmask))
+ return "%s/%s" % (str(self.ip), str(self.hostmask))
@property
def numhosts(self):
@property
def version(self):
- raise NotImplementedError('BaseNet has no version')
+ raise NotImplementedError("BaseNet has no version")
@property
def prefixlen(self):
"""
if not self._version == other._version:
- raise TypeError("%s and %s are not of the same version" % (
- str(self), str(other)))
+ raise TypeError(
+ "%s and %s are not of the same version" % (str(self), str(other))
+ )
if not isinstance(other, _BaseNet):
raise TypeError("%s is not a network object" % str(other))
if other not in self:
- raise ValueError('%s not contained in %s' % (str(other),
- str(self)))
+ raise ValueError("%s not contained in %s" % (str(other), str(self)))
if other == self:
return []
ret_addrs = []
# Make sure we're comparing the network of other.
- other = IPNetwork('%s/%s' % (str(other.network), str(other.prefixlen)),
- version=other._version)
+ other = IPNetwork(
+ "%s/%s" % (str(other.network), str(other.prefixlen)), 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 False, ('Error performing exclusion: '
- 's1: %s s2: %s other: %s' %
- (str(s1), str(s2), str(other)))
+ assert (
+ False
+ ), "Error performing exclusion: " "s1: %s s2: %s other: %s" % (
+ str(s1),
+ str(s2),
+ str(other),
+ )
if s1 == other:
ret_addrs.append(s2)
elif s2 == other:
ret_addrs.append(s1)
else:
# If we got here, there's a bug somewhere.
- assert False, ('Error performing exclusion: '
- 's1: %s s2: %s other: %s' %
- (str(s1), str(s2), str(other)))
+ assert False, "Error performing exclusion: " "s1: %s s2: %s other: %s" % (
+ str(s1),
+ str(s2),
+ str(other),
+ )
return sorted(ret_addrs, key=_BaseNet._get_networks_key)
if ip_int == (1 << prefixlen) - 1:
return prefixlen
else:
- raise NetmaskValueError('Bit pattern does not match /1*0*/')
+ raise NetmaskValueError("Bit pattern does not match /1*0*/")
def _prefix_from_prefix_string(self, prefixlen_str):
"""Turn a prefix length string into an integer.
if not (0 <= prefixlen <= self._max_prefixlen):
raise ValueError
except ValueError:
- raise NetmaskValueError('%s is not a valid prefix length' %
- prefixlen_str)
+ raise NetmaskValueError("%s is not a valid prefix length" % prefixlen_str)
return prefixlen
def _prefix_from_ip_string(self, ip_str):
try:
ip_int = self._ip_int_from_string(ip_str)
except AddressValueError:
- raise NetmaskValueError('%s is not a valid netmask' % ip_str)
+ raise NetmaskValueError("%s is not a valid netmask" % ip_str)
# Try matching a netmask (this would be /1*0*/ as a bitwise regexp).
# Note that the two ambiguous cases (all-ones and all-zeroes) are
try:
return self._prefix_from_ip_int(ip_int)
except NetmaskValueError:
- raise NetmaskValueError('%s is not a valid netmask' % ip_str)
+ raise NetmaskValueError("%s is not a valid netmask" % ip_str)
def iter_subnets(self, prefixlen_diff=1, new_prefix=None):
"""The subnets which join to make the current subnet.
if new_prefix is not None:
if new_prefix < self._prefixlen:
- raise ValueError('new prefix must be longer')
+ raise ValueError("new prefix must be longer")
if prefixlen_diff != 1:
- raise ValueError('cannot set prefixlen_diff and new_prefix')
+ raise ValueError("cannot set prefixlen_diff and new_prefix")
prefixlen_diff = new_prefix - self._prefixlen
if prefixlen_diff < 0:
- raise ValueError('prefix length diff must be > 0')
+ raise ValueError("prefix length diff must be > 0")
new_prefixlen = self._prefixlen + prefixlen_diff
if new_prefixlen > self._max_prefixlen:
raise ValueError(
- 'prefix length diff %d is invalid for netblock %s' % (
- new_prefixlen, str(self)))
+ "prefix length diff %d is invalid for netblock %s"
+ % (new_prefixlen, str(self))
+ )
- first = IPNetwork('%s/%s' % (str(self.network),
- str(self._prefixlen + prefixlen_diff)),
- version=self._version)
+ first = IPNetwork(
+ "%s/%s" % (str(self.network), str(self._prefixlen + prefixlen_diff)),
+ version=self._version,
+ )
yield first
current = first
if broadcast == self.broadcast:
return
new_addr = IPAddress(int(broadcast) + 1, version=self._version)
- current = IPNetwork('%s/%s' % (str(new_addr), str(new_prefixlen)),
- version=self._version)
+ current = IPNetwork(
+ "%s/%s" % (str(new_addr), str(new_prefixlen)), version=self._version
+ )
yield current
def masked(self):
"""Return the network object with the host bits masked out."""
- return IPNetwork('%s/%d' % (self.network, self._prefixlen),
- version=self._version)
+ return IPNetwork(
+ "%s/%d" % (self.network, self._prefixlen), version=self._version
+ )
def subnet(self, prefixlen_diff=1, new_prefix=None):
"""Return a list of subnets, rather than an iterator."""
if new_prefix is not None:
if new_prefix > self._prefixlen:
- raise ValueError('new prefix must be shorter')
+ raise ValueError("new prefix must be shorter")
if prefixlen_diff != 1:
- raise ValueError('cannot set prefixlen_diff and new_prefix')
+ 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' %
- (self.prefixlen, prefixlen_diff))
- return IPNetwork('%s/%s' % (str(self.network),
- str(self.prefixlen - prefixlen_diff)),
- version=self._version)
+ "current prefixlen is %d, cannot have a prefixlen_diff of %d"
+ % (self.prefixlen, prefixlen_diff)
+ )
+ return IPNetwork(
+ "%s/%s" % (str(self.network), str(self.prefixlen - prefixlen_diff)),
+ version=self._version,
+ )
# backwards compatibility
Subnet = subnet
# Equivalent to 255.255.255.255 or 32 bits of 1's.
_ALL_ONES = (2**IPV4LENGTH) - 1
- _DECIMAL_DIGITS = frozenset('0123456789')
+ _DECIMAL_DIGITS = frozenset("0123456789")
def __init__(self, address):
self._version = 4
AddressValueError: if ip_str isn't a valid IPv4 Address.
"""
- octets = ip_str.split('.')
+ octets = ip_str.split(".")
if len(octets) != 4:
raise AddressValueError(ip_str)
octet_int = int(octet_str, 10)
# Disallow leading zeroes, because no clear standard exists on
# whether these should be interpreted as decimal or octal.
- if octet_int > 255 or (octet_str[0] == '0' and len(octet_str) > 1):
+ if octet_int > 255 or (octet_str[0] == "0" and len(octet_str) > 1):
raise ValueError
return octet_int
"""
octets = []
- for _ in xrange(4):
+ for _ in range(4):
octets.insert(0, str(ip_int & 0xFF))
ip_int >>= 8
- return '.'.join(octets)
+ return ".".join(octets)
@property
def max_prefixlen(self):
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):
A boolean, True if the address is reserved per RFC 1918.
"""
- return (self in IPv4Network('10.0.0.0/8') or
- self in IPv4Network('172.16.0.0/12') or
- self in IPv4Network('192.168.0.0/16'))
+ return (
+ self in IPv4Network("10.0.0.0/8")
+ or self in IPv4Network("172.16.0.0/12")
+ or self in IPv4Network("192.168.0.0/16")
+ )
@property
def is_multicast(self):
See RFC 3171 for details.
"""
- return self in IPv4Network('224.0.0.0/4')
+ return self in IPv4Network("224.0.0.0/4")
@property
def is_unspecified(self):
RFC 5735 3.
"""
- return self in IPv4Network('0.0.0.0')
+ return self in IPv4Network("0.0.0.0")
@property
def is_loopback(self):
A boolean, True if the address is a loopback per RFC 3330.
"""
- return self in IPv4Network('127.0.0.0/8')
+ return self in IPv4Network("127.0.0.0/8")
@property
def is_link_local(self):
A boolean, True if the address is link-local per RFC 3927.
"""
- return self in IPv4Network('169.254.0.0/16')
+ return self in IPv4Network("169.254.0.0/16")
class IPv4Address(_BaseV4, _BaseIP):
_BaseV4.__init__(self, address)
# Efficient constructor from integer.
- if isinstance(address, (int, long)):
+ if isinstance(address, int):
self._ip = address
if address < 0 or address > self._ALL_ONES:
raise AddressValueError(address)
# Constructing from a packed address
if isinstance(address, Bytes):
try:
- self._ip, = struct.unpack('!I', address)
+ (self._ip,) = struct.unpack("!I", address)
except struct.error:
raise AddressValueError(address) # Wrong length.
return
_BaseV4.__init__(self, address)
# Constructing from an integer or packed bytes.
- if isinstance(address, (int, long, Bytes)):
+ if isinstance(address, (int, Bytes)):
self.ip = IPv4Address(address)
self._ip = self.ip._ip
self._prefixlen = self._max_prefixlen
# Assume input argument to be string or any object representation
# which converts into a formatted IP prefix string.
- addr = str(address).split('/')
+ addr = str(address).split("/")
if len(addr) > 2:
raise AddressValueError(address)
if strict:
if self.ip != self.network:
- raise ValueError('%s has host bits set' %
- self.ip)
+ raise ValueError("%s has host bits set" % self.ip)
if self._prefixlen == (self._max_prefixlen - 1):
self.iterhosts = self.__iter__
_ALL_ONES = (2**IPV6LENGTH) - 1
_HEXTET_COUNT = 8
- _HEX_DIGITS = frozenset('0123456789ABCDEFabcdef')
+ _HEX_DIGITS = frozenset("0123456789ABCDEFabcdef")
def __init__(self, address):
self._version = 6
AddressValueError: if ip_str isn't a valid IPv6 Address.
"""
- parts = ip_str.split(':')
+ parts = ip_str.split(":")
# An IPv6 address needs at least 2 colons (3 parts).
if len(parts) < 3:
raise AddressValueError(ip_str)
# If the address has an IPv4-style suffix, convert it to hexadecimal.
- if '.' in parts[-1]:
+ if "." in parts[-1]:
ipv4_int = IPv4Address(parts.pop())._ip
- parts.append('%x' % ((ipv4_int >> 16) & 0xFFFF))
- parts.append('%x' % (ipv4_int & 0xFFFF))
+ parts.append("%x" % ((ipv4_int >> 16) & 0xFFFF))
+ parts.append("%x" % (ipv4_int & 0xFFFF))
# An IPv6 address can't have more than 8 colons (9 parts).
if len(parts) > self._HEXTET_COUNT + 1:
# Disregarding the endpoints, find '::' with nothing in between.
# This indicates that a run of zeroes has been skipped.
try:
- skip_index, = (
- [i for i in xrange(1, len(parts) - 1) if not parts[i]] or
- [None])
+ (skip_index,) = [i for i in range(1, len(parts) - 1) if not parts[i]] or [
+ None
+ ]
except ValueError:
# Can't have more than one '::'
raise AddressValueError(ip_str)
try:
# Now, parse the hextets into a 128-bit integer.
ip_int = 0
- for i in xrange(parts_hi):
+ for i in range(parts_hi):
ip_int <<= 16
ip_int |= self._parse_hextet(parts[i])
ip_int <<= 16 * parts_skipped
- for i in xrange(-parts_lo, 0):
+ for i in range(-parts_lo, 0):
ip_int <<= 16
ip_int |= self._parse_hextet(parts[i])
return ip_int
doublecolon_start = -1
doublecolon_len = 0
for index in range(len(hextets)):
- if hextets[index] == '0':
+ if hextets[index] == "0":
doublecolon_len += 1
if doublecolon_start == -1:
# Start of a sequence of zeros.
doublecolon_start = -1
if best_doublecolon_len > 1:
- best_doublecolon_end = (best_doublecolon_start +
- best_doublecolon_len)
+ best_doublecolon_end = best_doublecolon_start + best_doublecolon_len
# For zeros at the end of the address.
if best_doublecolon_end == len(hextets):
- hextets += ['']
- hextets[best_doublecolon_start:best_doublecolon_end] = ['']
+ hextets += [""]
+ hextets[best_doublecolon_start:best_doublecolon_end] = [""]
# For zeros at the beginning of the address.
if best_doublecolon_start == 0:
- hextets = [''] + hextets
+ hextets = [""] + hextets
return hextets
ip_int = int(self._ip)
if ip_int > self._ALL_ONES:
- raise ValueError('IPv6 address is too large')
+ raise ValueError("IPv6 address is too large")
- hex_str = '%032x' % ip_int
+ hex_str = "%032x" % ip_int
hextets = []
for x in range(0, 32, 4):
- hextets.append('%x' % int(hex_str[x:x + 4], 16))
+ hextets.append("%x" % int(hex_str[x : x + 4], 16))
hextets = self._compress_hextets(hextets)
- return ':'.join(hextets)
+ return ":".join(hextets)
def _explode_shorthand_ip_string(self):
"""Expand a shortened IPv6 address.
ip_int = self._ip_int_from_string(ip_str)
parts = []
- for i in xrange(self._HEXTET_COUNT):
- parts.append('%04x' % (ip_int & 0xFFFF))
+ for i in range(self._HEXTET_COUNT):
+ parts.append("%04x" % (ip_int & 0xFFFF))
ip_int >>= 16
parts.reverse()
if isinstance(self, _BaseNet):
- return '%s/%d' % (':'.join(parts), self.prefixlen)
- return ':'.join(parts)
+ return "%s/%d" % (":".join(parts), self.prefixlen)
+ return ":".join(parts)
@property
def max_prefixlen(self):
See RFC 2373 2.7 for details.
"""
- return self in IPv6Network('ff00::/8')
+ return self in IPv6Network("ff00::/8")
@property
def is_reserved(self):
reserved IPv6 Network ranges.
"""
- return (self in IPv6Network('::/8') or
- self in IPv6Network('100::/8') or
- self in IPv6Network('200::/7') or
- self in IPv6Network('400::/6') or
- self in IPv6Network('800::/5') or
- self in IPv6Network('1000::/4') or
- self in IPv6Network('4000::/3') or
- self in IPv6Network('6000::/3') or
- self in IPv6Network('8000::/3') or
- self in IPv6Network('A000::/3') or
- self in IPv6Network('C000::/3') or
- self in IPv6Network('E000::/4') or
- self in IPv6Network('F000::/5') or
- self in IPv6Network('F800::/6') or
- self in IPv6Network('FE00::/9'))
+ return (
+ self in IPv6Network("::/8")
+ or self in IPv6Network("100::/8")
+ or self in IPv6Network("200::/7")
+ or self in IPv6Network("400::/6")
+ or self in IPv6Network("800::/5")
+ or self in IPv6Network("1000::/4")
+ or self in IPv6Network("4000::/3")
+ or self in IPv6Network("6000::/3")
+ or self in IPv6Network("8000::/3")
+ or self in IPv6Network("A000::/3")
+ or self in IPv6Network("C000::/3")
+ or self in IPv6Network("E000::/4")
+ or self in IPv6Network("F000::/5")
+ or self in IPv6Network("F800::/6")
+ or self in IPv6Network("FE00::/9")
+ )
@property
def is_unspecified(self):
RFC 2373 2.5.2.
"""
- return self._ip == 0 and getattr(self, '_prefixlen', 128) == 128
+ return self._ip == 0 and getattr(self, "_prefixlen", 128) == 128
@property
def is_loopback(self):
RFC 2373 2.5.3.
"""
- return self._ip == 1 and getattr(self, '_prefixlen', 128) == 128
+ return self._ip == 1 and getattr(self, "_prefixlen", 128) == 128
@property
def is_link_local(self):
A boolean, True if the address is reserved per RFC 4291.
"""
- return self in IPv6Network('fe80::/10')
+ return self in IPv6Network("fe80::/10")
@property
def is_site_local(self):
A boolean, True if the address is reserved per RFC 3513 2.5.6.
"""
- return self in IPv6Network('fec0::/10')
+ return self in IPv6Network("fec0::/10")
@property
def is_private(self):
A boolean, True if the address is reserved per RFC 4193.
"""
- return self in IPv6Network('fc00::/7')
+ return self in IPv6Network("fc00::/7")
@property
def ipv4_mapped(self):
"""
if (self._ip >> 96) != 0x20010000:
return None
- return (IPv4Address((self._ip >> 64) & 0xFFFFFFFF),
- IPv4Address(~self._ip & 0xFFFFFFFF))
+ return (
+ IPv4Address((self._ip >> 64) & 0xFFFFFFFF),
+ IPv4Address(~self._ip & 0xFFFFFFFF),
+ )
@property
def sixtofour(self):
class IPv6Address(_BaseV6, _BaseIP):
- """Represent and manipulate single IPv6 Addresses.
- """
+ """Represent and manipulate single IPv6 Addresses."""
def __init__(self, address):
"""Instantiate a new IPv6 address object.
_BaseV6.__init__(self, address)
# Efficient constructor from integer.
- if isinstance(address, (int, long)):
+ if isinstance(address, int):
self._ip = address
if address < 0 or address > self._ALL_ONES:
raise AddressValueError(address)
# Constructing from a packed address
if isinstance(address, Bytes):
try:
- hi, lo = struct.unpack('!QQ', address)
+ hi, lo = struct.unpack("!QQ", address)
except struct.error:
raise AddressValueError(address) # Wrong length.
self._ip = (hi << 64) | lo
# which converts into a formatted IP string.
addr_str = str(address)
if not addr_str:
- raise AddressValueError('')
+ raise AddressValueError("")
self._ip = self._ip_int_from_string(addr_str)
_BaseV6.__init__(self, address)
# Constructing from an integer or packed bytes.
- if isinstance(address, (int, long, Bytes)):
+ if isinstance(address, (int, Bytes)):
self.ip = IPv6Address(address)
self._ip = self.ip._ip
self._prefixlen = self._max_prefixlen
# Assume input argument to be string or any object representation
# which converts into a formatted IP prefix string.
- addr = str(address).split('/')
+ addr = str(address).split("/")
if len(addr) > 2:
raise AddressValueError(address)
if strict:
if self.ip != self.network:
- raise ValueError('%s has host bits set' %
- self.ip)
+ raise ValueError("%s has host bits set" % self.ip)
if self._prefixlen == (self._max_prefixlen - 1):
self.iterhosts = self.__iter__