mirror of
https://github.com/retspen/webvirtcloud
synced 2024-11-01 03:54:15 +00:00
IPy.py update to 1.0
This commit is contained in:
parent
e47f97facf
commit
1e22547b1a
1 changed files with 13 additions and 7 deletions
|
@ -6,7 +6,7 @@ Further Information might be available at:
|
|||
https://github.com/haypo/python-ipy
|
||||
"""
|
||||
|
||||
__version__ = '0.83'
|
||||
__version__ = '1.00'
|
||||
|
||||
import bisect
|
||||
import collections
|
||||
|
@ -21,7 +21,7 @@ IPv4ranges = {
|
|||
'00000000': 'PRIVATE', # 0/8
|
||||
'00001010': 'PRIVATE', # 10/8
|
||||
'0110010001': 'CARRIER_GRADE_NAT', # 100.64/10
|
||||
'01111111': 'PRIVATE', # 127.0/8
|
||||
'01111111': 'LOOPBACK', # 127.0/8
|
||||
'1': 'PUBLIC', # fall back
|
||||
'1010100111111110': 'PRIVATE', # 169.254/16
|
||||
'101011000001': 'PRIVATE', # 172.16/12
|
||||
|
@ -243,7 +243,7 @@ class IPint(object):
|
|||
else:
|
||||
raise ValueError("can't parse")
|
||||
|
||||
(self.ip, parsedVersion) = parseAddress(ip)
|
||||
(self.ip, parsedVersion) = parseAddress(ip, ipversion)
|
||||
if ipversion == 0:
|
||||
ipversion = parsedVersion
|
||||
if prefixlen == -1:
|
||||
|
@ -473,7 +473,7 @@ class IPint(object):
|
|||
"""Return a description of the IP type ('PRIVATE', 'RESERVED', etc).
|
||||
|
||||
>>> print(IP('127.0.0.1').iptype())
|
||||
PRIVATE
|
||||
LOOPBACK
|
||||
>>> print(IP('192.168.1.1').iptype())
|
||||
PRIVATE
|
||||
>>> print(IP('195.185.1.2').iptype())
|
||||
|
@ -553,6 +553,9 @@ class IPint(object):
|
|||
"""
|
||||
return True
|
||||
|
||||
def __bool__(self):
|
||||
return self.__nonzero__()
|
||||
|
||||
def __len__(self):
|
||||
"""
|
||||
Return the length of a subnet.
|
||||
|
@ -757,6 +760,9 @@ class IPint(object):
|
|||
def __lt__(self, other):
|
||||
return self.__cmp__(other) < 0
|
||||
|
||||
def __le__(self, other):
|
||||
return self.__cmp__(other) <= 0
|
||||
|
||||
def __hash__(self):
|
||||
"""Called for the key object for dictionary operations, and by
|
||||
the built-in function hash(). Should return a 32-bit integer
|
||||
|
@ -1328,7 +1334,7 @@ def _parseAddressIPv6(ipstr):
|
|||
return value
|
||||
|
||||
|
||||
def parseAddress(ipstr):
|
||||
def parseAddress(ipstr, ipversion=0):
|
||||
"""
|
||||
Parse a string and return the corresponding IP address (as integer)
|
||||
and a guess of the IP version.
|
||||
|
@ -1397,7 +1403,7 @@ def parseAddress(ipstr):
|
|||
# assume IPv6 in pure hexadecimal notation
|
||||
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')
|
||||
bytes = ipstr.split('.')
|
||||
if len(bytes) > 4:
|
||||
|
@ -1415,7 +1421,7 @@ def parseAddress(ipstr):
|
|||
# will be interpreted as IPv4 first byte
|
||||
if intval > MAX_IPV6_ADDRESS:
|
||||
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)
|
||||
else:
|
||||
return (intval, 6)
|
||||
|
|
Loading…
Reference in a new issue