1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2025-07-31 12:41:08 +00:00

lint with black python. convert f style strings to old one. some small fixes

This commit is contained in:
catborise 2020-11-05 12:34:31 +03:00
parent c20c353a40
commit 508e3609be
54 changed files with 2123 additions and 1824 deletions

View file

@ -6,7 +6,7 @@ Further Information might be available at:
https://github.com/haypo/python-ipy
"""
__version__ = '1.00'
__version__ = "1.00"
import bisect
import collections
@ -17,16 +17,16 @@ import types
# this should include www.iana.org/assignments/ipv4-address-space
# and www.iana.org/assignments/multicast-addresses
IPv4ranges = {
'0': 'PUBLIC', # fall back
'00000000': 'PRIVATE', # 0/8
'00001010': 'PRIVATE', # 10/8
'0110010001': 'CARRIER_GRADE_NAT', # 100.64/10
'01111111': 'LOOPBACK', # 127.0/8
'1': 'PUBLIC', # fall back
'1010100111111110': 'PRIVATE', # 169.254/16
'101011000001': 'PRIVATE', # 172.16/12
'1100000010101000': 'PRIVATE', # 192.168/16
'111': 'RESERVED', # 224/3
"0": "PUBLIC", # fall back
"00000000": "PRIVATE", # 0/8
"00001010": "PRIVATE", # 10/8
"0110010001": "CARRIER_GRADE_NAT", # 100.64/10
"01111111": "LOOPBACK", # 127.0/8
"1": "PUBLIC", # fall back
"1010100111111110": "PRIVATE", # 169.254/16
"101011000001": "PRIVATE", # 172.16/12
"1100000010101000": "PRIVATE", # 192.168/16
"111": "RESERVED", # 224/3
}
# Definition of the Ranges for IPv6 IPs
@ -34,92 +34,92 @@ IPv4ranges = {
# http://www.iana.org/assignments/ipv6-unicast-address-assignments/
# http://www.iana.org/assignments/ipv6-multicast-addresses/
IPv6ranges = {
'00000000': 'RESERVED', # ::/8
'0' * 96: 'RESERVED', # ::/96 Formerly IPV4COMP [RFC4291]
'0' * 128: 'UNSPECIFIED', # ::/128
'0' * 127 + '1': 'LOOPBACK', # ::1/128
'0' * 80 + '1' * 16: 'IPV4MAP', # ::ffff:0:0/96
'00000000011001001111111110011011' + '0' * 64: 'WKP46TRANS', # 0064:ff9b::/96 Well-Known-Prefix [RFC6052]
'00000001': 'UNASSIGNED', # 0100::/8
'0000001': 'RESERVED', # 0200::/7 Formerly NSAP [RFC4048]
'0000010': 'RESERVED', # 0400::/7 Formerly IPX [RFC3513]
'0000011': 'RESERVED', # 0600::/7
'00001': 'RESERVED', # 0800::/5
'0001': 'RESERVED', # 1000::/4
'001': 'GLOBAL-UNICAST', # 2000::/3 [RFC4291]
'00100000000000010000000': 'SPECIALPURPOSE', # 2001::/23 [RFC4773]
'00100000000000010000000000000000': 'TEREDO', # 2001::/32 [RFC4380]
'00100000000000010000000000000010' + '0' * 16: 'BMWG', # 2001:0002::/48 Benchmarking [RFC5180]
'0010000000000001000000000001': 'ORCHID', # 2001:0010::/28 (Temp until 2014-03-21) [RFC4843]
'00100000000000010000001': 'ALLOCATED APNIC', # 2001:0200::/23
'00100000000000010000010': 'ALLOCATED ARIN', # 2001:0400::/23
'00100000000000010000011': 'ALLOCATED RIPE NCC', # 2001:0600::/23
'00100000000000010000100': 'ALLOCATED RIPE NCC', # 2001:0800::/23
'00100000000000010000101': 'ALLOCATED RIPE NCC', # 2001:0a00::/23
'00100000000000010000110': 'ALLOCATED APNIC', # 2001:0c00::/23
'00100000000000010000110110111000': 'DOCUMENTATION', # 2001:0db8::/32 [RFC3849]
'00100000000000010000111': 'ALLOCATED APNIC', # 2001:0e00::/23
'00100000000000010001001': 'ALLOCATED LACNIC', # 2001:1200::/23
'00100000000000010001010': 'ALLOCATED RIPE NCC', # 2001:1400::/23
'00100000000000010001011': 'ALLOCATED RIPE NCC', # 2001:1600::/23
'00100000000000010001100': 'ALLOCATED ARIN', # 2001:1800::/23
'00100000000000010001101': 'ALLOCATED RIPE NCC', # 2001:1a00::/23
'0010000000000001000111': 'ALLOCATED RIPE NCC', # 2001:1c00::/22
'00100000000000010010': 'ALLOCATED RIPE NCC', # 2001:2000::/20
'001000000000000100110': 'ALLOCATED RIPE NCC', # 2001:3000::/21
'0010000000000001001110': 'ALLOCATED RIPE NCC', # 2001:3800::/22
'0010000000000001001111': 'RESERVED', # 2001:3c00::/22 Possible future allocation to RIPE NCC
'00100000000000010100000': 'ALLOCATED RIPE NCC', # 2001:4000::/23
'00100000000000010100001': 'ALLOCATED AFRINIC', # 2001:4200::/23
'00100000000000010100010': 'ALLOCATED APNIC', # 2001:4400::/23
'00100000000000010100011': 'ALLOCATED RIPE NCC', # 2001:4600::/23
'00100000000000010100100': 'ALLOCATED ARIN', # 2001:4800::/23
'00100000000000010100101': 'ALLOCATED RIPE NCC', # 2001:4a00::/23
'00100000000000010100110': 'ALLOCATED RIPE NCC', # 2001:4c00::/23
'00100000000000010101': 'ALLOCATED RIPE NCC', # 2001:5000::/20
'0010000000000001100': 'ALLOCATED APNIC', # 2001:8000::/19
'00100000000000011010': 'ALLOCATED APNIC', # 2001:a000::/20
'00100000000000011011': 'ALLOCATED APNIC', # 2001:b000::/20
'0010000000000010': '6TO4', # 2002::/16 "6to4" [RFC3056]
'001000000000001100': 'ALLOCATED RIPE NCC', # 2003::/18
'001001000000': 'ALLOCATED APNIC', # 2400::/12
'001001100000': 'ALLOCATED ARIN', # 2600::/12
'00100110000100000000000': 'ALLOCATED ARIN', # 2610::/23
'00100110001000000000000': 'ALLOCATED ARIN', # 2620::/23
'001010000000': 'ALLOCATED LACNIC', # 2800::/12
'001010100000': 'ALLOCATED RIPE NCC', # 2a00::/12
'001011000000': 'ALLOCATED AFRINIC', # 2c00::/12
'00101101': 'RESERVED', # 2d00::/8
'0010111': 'RESERVED', # 2e00::/7
'0011': 'RESERVED', # 3000::/4
'010': 'RESERVED', # 4000::/3
'011': 'RESERVED', # 6000::/3
'100': 'RESERVED', # 8000::/3
'101': 'RESERVED', # a000::/3
'110': 'RESERVED', # c000::/3
'1110': 'RESERVED', # e000::/4
'11110': 'RESERVED', # f000::/5
'111110': 'RESERVED', # f800::/6
'1111110': 'ULA', # fc00::/7 [RFC4193]
'111111100': 'RESERVED', # fe00::/9
'1111111010': 'LINKLOCAL', # fe80::/10
'1111111011': 'RESERVED', # fec0::/10 Formerly SITELOCAL [RFC4291]
'11111111': 'MULTICAST', # ff00::/8
'1111111100000001': 'NODE-LOCAL MULTICAST', # ff01::/16
'1111111100000010': 'LINK-LOCAL MULTICAST', # ff02::/16
'1111111100000100': 'ADMIN-LOCAL MULTICAST', # ff04::/16
'1111111100000101': 'SITE-LOCAL MULTICAST', # ff05::/16
'1111111100001000': 'ORG-LOCAL MULTICAST', # ff08::/16
'1111111100001110': 'GLOBAL MULTICAST', # ff0e::/16
'1111111100001111': 'RESERVED MULTICAST', # ff0f::/16
'111111110011': 'PREFIX-BASED MULTICAST', # ff30::/12 [RFC3306]
'111111110111': 'RP-EMBEDDED MULTICAST', # ff70::/12 [RFC3956]
"00000000": "RESERVED", # ::/8
"0" * 96: "RESERVED", # ::/96 Formerly IPV4COMP [RFC4291]
"0" * 128: "UNSPECIFIED", # ::/128
"0" * 127 + "1": "LOOPBACK", # ::1/128
"0" * 80 + "1" * 16: "IPV4MAP", # ::ffff:0:0/96
"00000000011001001111111110011011" + "0" * 64: "WKP46TRANS", # 0064:ff9b::/96 Well-Known-Prefix [RFC6052]
"00000001": "UNASSIGNED", # 0100::/8
"0000001": "RESERVED", # 0200::/7 Formerly NSAP [RFC4048]
"0000010": "RESERVED", # 0400::/7 Formerly IPX [RFC3513]
"0000011": "RESERVED", # 0600::/7
"00001": "RESERVED", # 0800::/5
"0001": "RESERVED", # 1000::/4
"001": "GLOBAL-UNICAST", # 2000::/3 [RFC4291]
"00100000000000010000000": "SPECIALPURPOSE", # 2001::/23 [RFC4773]
"00100000000000010000000000000000": "TEREDO", # 2001::/32 [RFC4380]
"00100000000000010000000000000010" + "0" * 16: "BMWG", # 2001:0002::/48 Benchmarking [RFC5180]
"0010000000000001000000000001": "ORCHID", # 2001:0010::/28 (Temp until 2014-03-21) [RFC4843]
"00100000000000010000001": "ALLOCATED APNIC", # 2001:0200::/23
"00100000000000010000010": "ALLOCATED ARIN", # 2001:0400::/23
"00100000000000010000011": "ALLOCATED RIPE NCC", # 2001:0600::/23
"00100000000000010000100": "ALLOCATED RIPE NCC", # 2001:0800::/23
"00100000000000010000101": "ALLOCATED RIPE NCC", # 2001:0a00::/23
"00100000000000010000110": "ALLOCATED APNIC", # 2001:0c00::/23
"00100000000000010000110110111000": "DOCUMENTATION", # 2001:0db8::/32 [RFC3849]
"00100000000000010000111": "ALLOCATED APNIC", # 2001:0e00::/23
"00100000000000010001001": "ALLOCATED LACNIC", # 2001:1200::/23
"00100000000000010001010": "ALLOCATED RIPE NCC", # 2001:1400::/23
"00100000000000010001011": "ALLOCATED RIPE NCC", # 2001:1600::/23
"00100000000000010001100": "ALLOCATED ARIN", # 2001:1800::/23
"00100000000000010001101": "ALLOCATED RIPE NCC", # 2001:1a00::/23
"0010000000000001000111": "ALLOCATED RIPE NCC", # 2001:1c00::/22
"00100000000000010010": "ALLOCATED RIPE NCC", # 2001:2000::/20
"001000000000000100110": "ALLOCATED RIPE NCC", # 2001:3000::/21
"0010000000000001001110": "ALLOCATED RIPE NCC", # 2001:3800::/22
"0010000000000001001111": "RESERVED", # 2001:3c00::/22 Possible future allocation to RIPE NCC
"00100000000000010100000": "ALLOCATED RIPE NCC", # 2001:4000::/23
"00100000000000010100001": "ALLOCATED AFRINIC", # 2001:4200::/23
"00100000000000010100010": "ALLOCATED APNIC", # 2001:4400::/23
"00100000000000010100011": "ALLOCATED RIPE NCC", # 2001:4600::/23
"00100000000000010100100": "ALLOCATED ARIN", # 2001:4800::/23
"00100000000000010100101": "ALLOCATED RIPE NCC", # 2001:4a00::/23
"00100000000000010100110": "ALLOCATED RIPE NCC", # 2001:4c00::/23
"00100000000000010101": "ALLOCATED RIPE NCC", # 2001:5000::/20
"0010000000000001100": "ALLOCATED APNIC", # 2001:8000::/19
"00100000000000011010": "ALLOCATED APNIC", # 2001:a000::/20
"00100000000000011011": "ALLOCATED APNIC", # 2001:b000::/20
"0010000000000010": "6TO4", # 2002::/16 "6to4" [RFC3056]
"001000000000001100": "ALLOCATED RIPE NCC", # 2003::/18
"001001000000": "ALLOCATED APNIC", # 2400::/12
"001001100000": "ALLOCATED ARIN", # 2600::/12
"00100110000100000000000": "ALLOCATED ARIN", # 2610::/23
"00100110001000000000000": "ALLOCATED ARIN", # 2620::/23
"001010000000": "ALLOCATED LACNIC", # 2800::/12
"001010100000": "ALLOCATED RIPE NCC", # 2a00::/12
"001011000000": "ALLOCATED AFRINIC", # 2c00::/12
"00101101": "RESERVED", # 2d00::/8
"0010111": "RESERVED", # 2e00::/7
"0011": "RESERVED", # 3000::/4
"010": "RESERVED", # 4000::/3
"011": "RESERVED", # 6000::/3
"100": "RESERVED", # 8000::/3
"101": "RESERVED", # a000::/3
"110": "RESERVED", # c000::/3
"1110": "RESERVED", # e000::/4
"11110": "RESERVED", # f000::/5
"111110": "RESERVED", # f800::/6
"1111110": "ULA", # fc00::/7 [RFC4193]
"111111100": "RESERVED", # fe00::/9
"1111111010": "LINKLOCAL", # fe80::/10
"1111111011": "RESERVED", # fec0::/10 Formerly SITELOCAL [RFC4291]
"11111111": "MULTICAST", # ff00::/8
"1111111100000001": "NODE-LOCAL MULTICAST", # ff01::/16
"1111111100000010": "LINK-LOCAL MULTICAST", # ff02::/16
"1111111100000100": "ADMIN-LOCAL MULTICAST", # ff04::/16
"1111111100000101": "SITE-LOCAL MULTICAST", # ff05::/16
"1111111100001000": "ORG-LOCAL MULTICAST", # ff08::/16
"1111111100001110": "GLOBAL MULTICAST", # ff0e::/16
"1111111100001111": "RESERVED MULTICAST", # ff0f::/16
"111111110011": "PREFIX-BASED MULTICAST", # ff30::/12 [RFC3306]
"111111110111": "RP-EMBEDDED MULTICAST", # ff70::/12 [RFC3956]
}
MAX_IPV4_ADDRESS = 0xffffffff
MAX_IPV6_ADDRESS = 0xffffffffffffffffffffffffffffffff
IPV6_TEST_MAP = 0xffffffffffffffffffffffff00000000
IPV6_MAP_MASK = 0x00000000000000000000ffff00000000
MAX_IPV4_ADDRESS = 0xFFFFFFFF
MAX_IPV6_ADDRESS = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
IPV6_TEST_MAP = 0xFFFFFFFFFFFFFFFFFFFFFFFF00000000
IPV6_MAP_MASK = 0x00000000000000000000FFFF00000000
if sys.version_info >= (3,):
INT_TYPES = (int,)
@ -202,7 +202,7 @@ class IPint(object):
elif isinstance(data, STR_TYPES):
# TODO: refactor me!
# splitting of a string into IP and prefixlen et. al.
x = data.split('-')
x = data.split("-")
if len(x) == 2:
# a.b.c.0-a.b.c.255 specification ?
(ip, last) = x
@ -219,10 +219,10 @@ class IPint(object):
# make sure the broadcast is the same as the last ip
# otherwise it will return /16 for something like:
# 192.168.0.0-192.168.191.255
if IP('%s/%s' % (ip, 32 - netbits)).broadcast().int() != last:
if IP("%s/%s" % (ip, 32 - netbits)).broadcast().int() != last:
raise ValueError("the range %s is not on a network boundary." % data)
elif len(x) == 1:
x = data.split('/')
x = data.split("/")
# if no prefix is given use defaults
if len(x) == 1:
ip = x[0]
@ -231,7 +231,7 @@ class IPint(object):
raise ValueError("only one '/' allowed in IP Address")
else:
(ip, prefixlen) = x
if prefixlen.find('.') != -1:
if prefixlen.find(".") != -1:
# check if the user might have used a netmask like
# a.b.c.d/255.255.255.0
(netmask, vers) = parseAddress(prefixlen)
@ -255,8 +255,7 @@ class IPint(object):
if make_net:
self.ip = self.ip & _prefixlenToNetmask(self._prefixlen, self._ipversion)
if not _checkNetaddrWorksWithPrefixlen(self.ip,
self._prefixlen, self._ipversion):
if not _checkNetaddrWorksWithPrefixlen(self.ip, self._prefixlen, self._ipversion):
raise ValueError("%s has invalid prefix length (%s)" % (repr(self), self._prefixlen))
else:
raise TypeError("Unsupported data type: %s" % type(data))
@ -314,8 +313,7 @@ class IPint(object):
want == 3 -lastip 1.2.3.0-1.2.3.255
"""
if (self._ipversion == 4 and self._prefixlen == 32) or \
(self._ipversion == 6 and self._prefixlen == 128):
if (self._ipversion == 4 and self._prefixlen == 32) or (self._ipversion == 6 and self._prefixlen == 128):
if self.NoPrefixForSingleIp:
want = 0
if want is None:
@ -335,7 +333,7 @@ class IPint(object):
# default
return "/%d" % (self._prefixlen)
else:
return ''
return ""
# We have different flavours to convert to:
# strFullsize 127.0.0.1 2001:0658:022a:cafe:0200:c0ff:fe8d:08fa
@ -357,7 +355,7 @@ class IPint(object):
if self.WantPrefixLen is None and wantprefixlen is None:
wantprefixlen = 0
ret = _intToBin(self.ip)
return '0' * (bits - len(ret)) + ret + self._printPrefix(wantprefixlen)
return "0" * (bits - len(ret)) + ret + self._printPrefix(wantprefixlen)
def strCompressed(self, wantprefixlen=None):
"""Return a string representation in compressed format using '::' Notation.
@ -376,12 +374,12 @@ class IPint(object):
if self._ipversion == 4:
return self.strFullsize(wantprefixlen)
else:
if self.ip >> 32 == 0xffff:
if self.ip >> 32 == 0xFFFF:
ipv4 = intToIp(self.ip & MAX_IPV4_ADDRESS, 4)
text = "::ffff:" + ipv4 + self._printPrefix(wantprefixlen)
return text
# find the longest sequence of '0'
hextets = [int(x, 16) for x in self.strFullsize(0).split(':')]
hextets = [int(x, 16) for x in self.strFullsize(0).split(":")]
# every element of followingzeros will contain the number of zeros
# following the corresponding element of hextets
followingzeros = [0] * 8
@ -392,15 +390,15 @@ class IPint(object):
if max(followingzeros) > 1:
# genererate string with the longest number of zeros cut out
# now we need hextets as strings
hextets = [x for x in self.strNormal(0).split(':')]
while compressionpos < len(hextets) and hextets[compressionpos] == '0':
del (hextets[compressionpos])
hextets.insert(compressionpos, '')
hextets = [x for x in self.strNormal(0).split(":")]
while compressionpos < len(hextets) and hextets[compressionpos] == "0":
del hextets[compressionpos]
hextets.insert(compressionpos, "")
if compressionpos + 1 >= len(hextets):
hextets.append('')
hextets.append("")
if compressionpos == 0:
hextets = [''] + hextets
return ':'.join(hextets) + self._printPrefix(wantprefixlen)
hextets = [""] + hextets
return ":".join(hextets) + self._printPrefix(wantprefixlen)
else:
return self.strNormal(0) + self._printPrefix(wantprefixlen)
@ -419,7 +417,7 @@ class IPint(object):
if self._ipversion == 4:
ret = self.strFullsize(0)
elif self._ipversion == 6:
ret = ':'.join(["%x" % x for x in [int(x, 16) for x in self.strFullsize(0).split(':')]])
ret = ":".join(["%x" % x for x in [int(x, 16) for x in self.strFullsize(0).split(":")]])
else:
raise ValueError("only IPv4 and IPv6 supported")
@ -451,7 +449,7 @@ class IPint(object):
if self.WantPrefixLen is None and wantprefixlen is None:
wantprefixlen = 0
x = '0x%x' % self.ip
x = "0x%x" % self.ip
return x + self._printPrefix(wantprefixlen)
def strDec(self, wantprefixlen=None):
@ -466,7 +464,7 @@ class IPint(object):
if self.WantPrefixLen is None and wantprefixlen is None:
wantprefixlen = 0
x = '%d' % self.ip
x = "%d" % self.ip
return x + self._printPrefix(wantprefixlen)
def iptype(self):
@ -581,10 +579,8 @@ class IPint(object):
raise ValueError("Only adjacent networks can be added together.")
ret = IP(self.int(), ipversion=self._ipversion)
ret._prefixlen = self.prefixlen() - 1
if not _checkNetaddrWorksWithPrefixlen(ret.ip, ret._prefixlen,
ret._ipversion):
raise ValueError("The resulting %s has invalid prefix length (%s)"
% (repr(ret), ret._prefixlen))
if not _checkNetaddrWorksWithPrefixlen(ret.ip, ret._prefixlen, ret._ipversion):
raise ValueError("The resulting %s has invalid prefix length (%s)" % (repr(ret), ret._prefixlen))
return ret
def __sub__(self, other):
@ -692,7 +688,7 @@ class IPint(object):
IP('10.0.0.0/24')
"""
return ("IPint('%s')" % (self.strCompressed(1)))
return "IPint('%s')" % (self.strCompressed(1))
def __cmp__(self, other):
"""Called by comparison operations.
@ -777,7 +773,7 @@ class IPint(object):
thehash = int(-1)
ip = self.ip
while ip > 0:
thehash = thehash ^ (ip & 0x7fffffff)
thehash = thehash ^ (ip & 0x7FFFFFFF)
ip = ip >> 32
thehash = thehash ^ self._prefixlen
return int(thehash)
@ -811,17 +807,17 @@ class IP(IPint):
>>> IP('10.0.0.0/8').netmask()
IP('255.0.0.0')
"""
"""
return IP(IPint.netmask(self), ipversion=self._ipversion)
def _getIPv4Map(self):
if self._ipversion != 6:
return None
if (self.ip >> 32) != 0xffff:
if (self.ip >> 32) != 0xFFFF:
return None
ipv4 = self.ip & MAX_IPV4_ADDRESS
if self._prefixlen != 128:
ipv4 = '%s/%s' % (ipv4, 32 - (128 - self._prefixlen))
ipv4 = "%s/%s" % (ipv4, 32 - (128 - self._prefixlen))
return IP(ipv4, ipversion=4)
def reverseNames(self):
@ -872,7 +868,7 @@ class IP(IPint):
raise NotImplementedError("can't create IPv6 reverse names at sub nibble level")
s = list(s)
s.reverse()
s = '.'.join(s)
s = ".".join(s)
first_nibble_index = int(32 - (self._prefixlen // 4)) * 2
return ["%s.ip6.arpa." % s[first_nibble_index:]]
else:
@ -897,32 +893,34 @@ class IP(IPint):
if self._ipversion == 4:
s = self.strFullsize(0)
s = s.split('.')
s = s.split(".")
s.reverse()
first_byte_index = int(4 - (self._prefixlen // 8))
if self._prefixlen % 8 != 0:
nibblepart = "%s-%s" % (
s[3 - (self._prefixlen // 8)], intToIp(self.ip + self.len() - 1, 4).split('.')[-1])
nibblepart += '.'
s[3 - (self._prefixlen // 8)],
intToIp(self.ip + self.len() - 1, 4).split(".")[-1],
)
nibblepart += "."
else:
nibblepart = ""
s = '.'.join(s[first_byte_index:])
s = ".".join(s[first_byte_index:])
return "%s%s.in-addr.arpa." % (nibblepart, s)
elif self._ipversion == 6:
ipv4 = self._getIPv4Map()
if ipv4 is not None:
return ipv4.reverseName()
s = '%032x' % self.ip
s = "%032x" % self.ip
if self._prefixlen % 4 != 0:
nibblepart = "%s-%x" % (s[self._prefixlen:], self.ip + self.len() - 1)
nibblepart += '.'
nibblepart = "%s-%x" % (s[self._prefixlen :], self.ip + self.len() - 1)
nibblepart += "."
else:
nibblepart = ""
s = list(s)
s.reverse()
s = '.'.join(s)
s = ".".join(s)
first_nibble_index = int(32 - (self._prefixlen // 4)) * 2
return "%s%s.ip6.arpa." % (nibblepart, s[first_nibble_index:])
else:
@ -937,9 +935,9 @@ class IP(IPint):
>>> print(IP('127.0.0.1').make_net('255.0.0.0'))
127.0.0.0/8
"""
if '/' in str(netmask):
if "/" in str(netmask):
raise ValueError("invalid netmask (%s)" % netmask)
return IP('%s/%s' % (self, netmask), make_net=True)
return IP("%s/%s" % (self, netmask), make_net=True)
def __getitem__(self, key):
"""Called to implement evaluation of self[key].
@ -968,7 +966,7 @@ class IP(IPint):
IP('10.0.0.0/8')
"""
return ("IP('%s')" % (self.strCompressed(1)))
return "IP('%s')" % (self.strCompressed(1))
def get_mac(self):
"""
@ -980,15 +978,15 @@ class IP(IPint):
"""
if self._ipversion != 6:
return None
if (self.ip & 0x20000ffff000000) != 0x20000fffe000000:
if (self.ip & 0x20000FFFF000000) != 0x20000FFFE000000:
return None
return '%02x:%02x:%02x:%02x:%02x:%02x' % (
(((self.ip >> 56) & 0xff) & 0xfd),
(self.ip >> 48) & 0xff,
(self.ip >> 40) & 0xff,
(self.ip >> 16) & 0xff,
(self.ip >> 8) & 0xff,
self.ip & 0xff,
return "%02x:%02x:%02x:%02x:%02x:%02x" % (
(((self.ip >> 56) & 0xFF) & 0xFD),
(self.ip >> 48) & 0xFF,
(self.ip >> 40) & 0xFF,
(self.ip >> 16) & 0xFF,
(self.ip >> 8) & 0xFF,
self.ip & 0xFF,
)
def v46map(self):
@ -1003,14 +1001,11 @@ class IP(IPint):
IP('192.168.1.1')
"""
if self._ipversion == 4:
return IP(str(IPV6_MAP_MASK + self.ip) +
"/%s" % (self._prefixlen + 96))
return IP(str(IPV6_MAP_MASK + self.ip) + "/%s" % (self._prefixlen + 96))
else:
if self.ip & IPV6_TEST_MAP == IPV6_MAP_MASK:
return IP(str(self.ip - IPV6_MAP_MASK) +
"/%s" % (self._prefixlen - 96))
raise ValueError("%s cannot be converted to an IPv4 address."
% repr(self))
return IP(str(self.ip - IPV6_MAP_MASK) + "/%s" % (self._prefixlen - 96))
raise ValueError("%s cannot be converted to an IPv4 address." % repr(self))
class IPSet(collections.MutableSet):
@ -1022,7 +1017,7 @@ class IPSet(collections.MutableSet):
# Make sure we only accept IP objects
for prefix in iterable:
if not isinstance(prefix, IP):
raise ValueError('Only IP objects can be added to an IPSet')
raise ValueError("Only IP objects can be added to an IPSet")
# Store and optimize
self.prefixes = iterable[:]
@ -1083,7 +1078,7 @@ class IPSet(collections.MutableSet):
return IPSet(result)
def __repr__(self):
return '%s([' % self.__class__.__name__ + ', '.join(map(repr, self.prefixes)) + '])'
return "%s([" % self.__class__.__name__ + ", ".join(map(repr, self.prefixes)) + "])"
def len(self):
return sum(prefix.len() for prefix in self.prefixes)
@ -1096,7 +1091,7 @@ class IPSet(collections.MutableSet):
# Check type
for prefix in value:
if not isinstance(prefix, IP):
raise ValueError('Only IP objects can be added to an IPSet')
raise ValueError("Only IP objects can be added to an IPSet")
# Append and optimize
self.prefixes.extend(value)
@ -1114,7 +1109,7 @@ class IPSet(collections.MutableSet):
# Remove
for del_prefix in value:
if not isinstance(del_prefix, IP):
raise ValueError('Only IP objects can be removed from an IPSet')
raise ValueError("Only IP objects can be removed from an IPSet")
# First check if this prefix contains anything in our list
found = False
@ -1134,7 +1129,7 @@ class IPSet(collections.MutableSet):
found = False
for i in range(len(self.prefixes)):
if del_prefix in self.prefixes[i]:
self.prefixes[i:i + 1] = self.prefixes[i] - del_prefix
self.prefixes[i : i + 1] = self.prefixes[i] - del_prefix
break
self.optimize()
@ -1279,13 +1274,13 @@ def _parseAddressIPv6(ipstr):
fill_pos = len(items)
index += 2
continue
pos = text.find(':')
pos = text.find(":")
if pos == 0:
# Invalid IPv6, eg. '1::2:'
raise ValueError("%r: Invalid IPv6 address" % ipstr)
if pos != -1:
items.append(text[:pos])
if text[pos:pos + 2] == "::":
if text[pos : pos + 2] == "::":
index += pos
else:
index += pos + 1
@ -1297,13 +1292,13 @@ def _parseAddressIPv6(ipstr):
items.append(text)
break
if items and '.' in items[-1]:
if items and "." in items[-1]:
# IPv6 ending with IPv4 like '::ffff:192.168.0.1'
if (fill_pos is not None) and not (fill_pos <= len(items) - 1):
# Invalid IPv6: 'ffff:192.168.0.1::'
raise ValueError("%r: Invalid IPv6 address: '::' after IPv4" % ipstr)
value = parseAddress(items[-1])[0]
items = items[:-1] + ["%04x" % (value >> 16), "%04x" % (value & 0xffff)]
items = items[:-1] + ["%04x" % (value >> 16), "%04x" % (value & 0xFFFF)]
# Expand fill_pos to fill with '0'
# ['1','2'] with fill_pos=1 => ['1', '0', '0', '0', '0', '0', '0', '2']
@ -1311,7 +1306,7 @@ def _parseAddressIPv6(ipstr):
diff = 8 - len(items)
if diff <= 0:
raise ValueError("%r: Invalid IPv6 address: '::' is not needed" % ipstr)
items = items[:fill_pos] + ['0'] * diff + items[fill_pos:]
items = items[:fill_pos] + ["0"] * diff + items[fill_pos:]
# Here we have a list of 8 strings
if len(items) != 8:
@ -1324,7 +1319,7 @@ def _parseAddressIPv6(ipstr):
for item in items:
try:
item = int(item, 16)
error = not (0 <= item <= 0xffff)
error = not (0 <= item <= 0xFFFF)
except ValueError:
error = True
if error:
@ -1388,7 +1383,7 @@ def parseAddress(ipstr, ipversion=0):
except ValueError:
intval = None
if ipstr.startswith('0x') and hexval is not None:
if ipstr.startswith("0x") and hexval is not None:
if hexval > MAX_IPV6_ADDRESS:
raise ValueError("IP Address can't be larger than %x: %x" % (MAX_IPV6_ADDRESS, hexval))
if hexval <= MAX_IPV4_ADDRESS:
@ -1396,19 +1391,19 @@ def parseAddress(ipstr, ipversion=0):
else:
return (hexval, 6)
if ipstr.find(':') != -1:
if ipstr.find(":") != -1:
return (_parseAddressIPv6(ipstr), 6)
elif len(ipstr) == 32 and hexval is not None:
# assume IPv6 in pure hexadecimal notation
return (hexval, 6)
elif ipstr.find('.') != -1 or (intval is not None and intval < 256 and ipversion != 6):
elif ipstr.find(".") != -1 or (intval is not None and intval < 256 and ipversion != 6):
# assume IPv4 ('127' gets interpreted as '127.0.0.0')
bytes = ipstr.split('.')
bytes = ipstr.split(".")
if len(bytes) > 4:
raise ValueError("IPv4 Address with more than 4 bytes")
bytes += ['0'] * (4 - len(bytes))
bytes += ["0"] * (4 - len(bytes))
bytes = [int(x) for x in bytes]
for x in bytes:
if x > 255 or x < 0:
@ -1438,12 +1433,12 @@ def intToIp(ip, version):
if ip < 0:
raise ValueError("IPs can't be negative: %d" % (ip))
ret = ''
ret = ""
if version == 4:
if ip > MAX_IPV4_ADDRESS:
raise ValueError("IPv4 Address can't be larger than %x: %x" % (MAX_IPV4_ADDRESS, ip))
for l in xrange(4):
ret = str(ip & 0xff) + '.' + ret
ret = str(ip & 0xFF) + "." + ret
ip = ip >> 8
ret = ret[:-1]
elif version == 6:
@ -1453,7 +1448,7 @@ def intToIp(ip, version):
for x in xrange(1, 33):
ret = l[-x] + ret
if x % 4 == 0:
ret = ':' + ret
ret = ":" + ret
ret = ret[1:]
else:
raise ValueError("only IPv4 and IPv6 supported")
@ -1494,10 +1489,24 @@ def _countFollowingZeros(l):
return 1 + _countFollowingZeros(l[1:])
_BitTable = {'0': '0000', '1': '0001', '2': '0010', '3': '0011',
'4': '0100', '5': '0101', '6': '0110', '7': '0111',
'8': '1000', '9': '1001', 'a': '1010', 'b': '1011',
'c': '1100', 'd': '1101', 'e': '1110', 'f': '1111'}
_BitTable = {
"0": "0000",
"1": "0001",
"2": "0010",
"3": "0011",
"4": "0100",
"5": "0101",
"6": "0110",
"7": "0111",
"8": "1000",
"9": "1001",
"a": "1010",
"b": "1011",
"c": "1100",
"d": "1101",
"e": "1110",
"f": "1111",
}
def _intToBin(val):
@ -1506,11 +1515,11 @@ def _intToBin(val):
if val < 0:
raise ValueError("Only positive values allowed")
s = "%x" % val
ret = ''
ret = ""
for x in s:
ret += _BitTable[x]
# remove leading zeros
while ret[0] == '0' and len(ret) > 1:
while ret[0] == "0" and len(ret) > 1:
ret = ret[1:]
return ret
@ -1595,7 +1604,7 @@ def _checkNetmask(netmask, masklen):
def _checkNetaddrWorksWithPrefixlen(net, prefixlen, version):
"""Check if a base addess of a network is compatible with a prefixlen"""
try:
return (net & _prefixlenToNetmask(prefixlen, version) == net)
return net & _prefixlenToNetmask(prefixlen, version) == net
except ValueError:
return False
@ -1637,8 +1646,8 @@ def _remove_subprefix(prefix, subprefix):
# Start cutting in half, recursively
prefixes = [
IP('%s/%d' % (prefix[0], prefix._prefixlen + 1)),
IP('%s/%d' % (prefix[int(prefix.len() / 2)], prefix._prefixlen + 1)),
IP("%s/%d" % (prefix[0], prefix._prefixlen + 1)),
IP("%s/%d" % (prefix[int(prefix.len() / 2)], prefix._prefixlen + 1)),
]
if subprefix in prefixes[0]:
return _remove_subprefix(prefixes[0], subprefix) + IPSet([prefixes[1]])