1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2025-01-12 08:25:18 +00:00

IPy.py update to 1.0

This commit is contained in:
catborise 2019-09-09 16:53:52 +03:00
parent e47f97facf
commit 1e22547b1a

View file

@ -6,7 +6,7 @@ Further Information might be available at:
https://github.com/haypo/python-ipy https://github.com/haypo/python-ipy
""" """
__version__ = '0.83' __version__ = '1.00'
import bisect import bisect
import collections import collections
@ -21,7 +21,7 @@ IPv4ranges = {
'00000000': 'PRIVATE', # 0/8 '00000000': 'PRIVATE', # 0/8
'00001010': 'PRIVATE', # 10/8 '00001010': 'PRIVATE', # 10/8
'0110010001': 'CARRIER_GRADE_NAT', # 100.64/10 '0110010001': 'CARRIER_GRADE_NAT', # 100.64/10
'01111111': 'PRIVATE', # 127.0/8 '01111111': 'LOOPBACK', # 127.0/8
'1': 'PUBLIC', # fall back '1': 'PUBLIC', # fall back
'1010100111111110': 'PRIVATE', # 169.254/16 '1010100111111110': 'PRIVATE', # 169.254/16
'101011000001': 'PRIVATE', # 172.16/12 '101011000001': 'PRIVATE', # 172.16/12
@ -243,7 +243,7 @@ class IPint(object):
else: else:
raise ValueError("can't parse") raise ValueError("can't parse")
(self.ip, parsedVersion) = parseAddress(ip) (self.ip, parsedVersion) = parseAddress(ip, ipversion)
if ipversion == 0: if ipversion == 0:
ipversion = parsedVersion ipversion = parsedVersion
if prefixlen == -1: if prefixlen == -1:
@ -473,7 +473,7 @@ class IPint(object):
"""Return a description of the IP type ('PRIVATE', 'RESERVED', etc). """Return a description of the IP type ('PRIVATE', 'RESERVED', etc).
>>> print(IP('127.0.0.1').iptype()) >>> print(IP('127.0.0.1').iptype())
PRIVATE LOOPBACK
>>> print(IP('192.168.1.1').iptype()) >>> print(IP('192.168.1.1').iptype())
PRIVATE PRIVATE
>>> print(IP('195.185.1.2').iptype()) >>> print(IP('195.185.1.2').iptype())
@ -553,6 +553,9 @@ class IPint(object):
""" """
return True return True
def __bool__(self):
return self.__nonzero__()
def __len__(self): def __len__(self):
""" """
Return the length of a subnet. Return the length of a subnet.
@ -757,6 +760,9 @@ class IPint(object):
def __lt__(self, other): def __lt__(self, other):
return self.__cmp__(other) < 0 return self.__cmp__(other) < 0
def __le__(self, other):
return self.__cmp__(other) <= 0
def __hash__(self): def __hash__(self):
"""Called for the key object for dictionary operations, and by """Called for the key object for dictionary operations, and by
the built-in function hash(). Should return a 32-bit integer the built-in function hash(). Should return a 32-bit integer
@ -1328,7 +1334,7 @@ def _parseAddressIPv6(ipstr):
return value return value
def parseAddress(ipstr): def parseAddress(ipstr, ipversion=0):
""" """
Parse a string and return the corresponding IP address (as integer) Parse a string and return the corresponding IP address (as integer)
and a guess of the IP version. and a guess of the IP version.
@ -1397,7 +1403,7 @@ def parseAddress(ipstr):
# assume IPv6 in pure hexadecimal notation # assume IPv6 in pure hexadecimal notation
return (hexval, 6) return (hexval, 6)
elif ipstr.find('.') != -1 or (intval is not None and intval < 256): 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') # assume IPv4 ('127' gets interpreted as '127.0.0.0')
bytes = ipstr.split('.') bytes = ipstr.split('.')
if len(bytes) > 4: if len(bytes) > 4:
@ -1415,7 +1421,7 @@ def parseAddress(ipstr):
# will be interpreted as IPv4 first byte # will be interpreted as IPv4 first byte
if intval > MAX_IPV6_ADDRESS: if intval > MAX_IPV6_ADDRESS:
raise ValueError("IP Address can't be larger than %x: %x" % (MAX_IPV6_ADDRESS, intval)) raise ValueError("IP Address can't be larger than %x: %x" % (MAX_IPV6_ADDRESS, intval))
if intval <= MAX_IPV4_ADDRESS: if intval <= MAX_IPV4_ADDRESS and ipversion != 6:
return (intval, 4) return (intval, 4)
else: else:
return (intval, 6) return (intval, 6)