|
@@ -30,12 +30,20 @@ class IPAddress:
|
|
|
|
|
|
return IPAddress(list(numeric.to_bytes(4, "big")))
|
|
|
|
|
|
+ @staticmethod
|
|
|
+ def is_ipv4(ip: str):
|
|
|
+ match = re.match("^" + IPAddress.IP_REGEXP + "$", ip)
|
|
|
+ return True if match else False
|
|
|
+
|
|
|
def to_int(self):
|
|
|
return self.ipnum
|
|
|
|
|
|
def is_private(self):
|
|
|
return ReservedIPBlocks.is_private(self)
|
|
|
|
|
|
+ def get_private_segment(self):
|
|
|
+ return ReservedIPBlocks.get_private_segment(self)
|
|
|
+
|
|
|
def is_localhost(self):
|
|
|
return ReservedIPBlocks.is_localhost(self)
|
|
|
|
|
@@ -74,6 +82,14 @@ class IPAddress:
|
|
|
|
|
|
return self.ipnum < other.ipnum
|
|
|
|
|
|
+ def __gt__(self, other):
|
|
|
+ if other is None:
|
|
|
+ raise TypeError("Cannot compare to None")
|
|
|
+ if not isinstance(other, IPAddress):
|
|
|
+ raise NotImplemented # maybe other can compare to self
|
|
|
+
|
|
|
+ return self.ipnum > other.ipnum
|
|
|
+
|
|
|
class IPAddressBlock:
|
|
|
CIDR_REGEXP = IPAddress.IP_REGEXP + r"(\/(3[0-2]|[12]?\d)|)?"
|
|
|
|
|
@@ -132,6 +148,15 @@ class ReservedIPBlocks:
|
|
|
def is_private(ip):
|
|
|
return any(ip in block for block in ReservedIPBlocks.PRIVATE_IP_SEGMENTS)
|
|
|
|
|
|
+ @staticmethod
|
|
|
+ def get_private_segment(ip):
|
|
|
+ if not ReservedIPBlocks.is_private(ip):
|
|
|
+ raise ValueError("%s is not part of a private IP segment" % ip)
|
|
|
+
|
|
|
+ for block in ReservedIPBlocks.PRIVATE_IP_SEGMENTS:
|
|
|
+ if ip in block:
|
|
|
+ return block
|
|
|
+
|
|
|
@staticmethod
|
|
|
def is_localhost(ip):
|
|
|
return ip in ReservedIPBlocks.LOCALHOST_SEGMENT
|