Skip to content

Commit 523b947

Browse files
committed
slips_utils: support giving str ips to is_private_ip()
1 parent 0af7ced commit 523b947

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

slips_files/common/slips_utils.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
import sys
1717
import ipaddress
1818
import aid_hash
19-
from typing import Any, Optional
19+
from typing import (
20+
Any,
21+
Optional,
22+
Union,
23+
)
2024
from dataclasses import is_dataclass, asdict
2125
from enum import Enum
2226

@@ -390,8 +394,15 @@ def is_port_in_use(self, port: int) -> bool:
390394
sock.close()
391395
return True
392396

393-
def is_private_ip(self, ip_obj: ipaddress) -> bool:
394-
return ip_obj and ip_obj.is_private
397+
def is_private_ip(self, ip: Union[ipaddress, str]) -> bool:
398+
ip_classes = {ipaddress.IPv4Address, ipaddress.IPv6Address}
399+
for class_ in ip_classes:
400+
if isinstance(ip, class_):
401+
return ip and ip.is_private
402+
403+
# convert the given str ip to obj
404+
ip_obj = ipaddress.ip_address(ip)
405+
return ip_obj.is_private
395406

396407
def is_ignored_ip(self, ip: str) -> bool:
397408
"""

0 commit comments

Comments
 (0)