@@ -26,7 +26,7 @@ def ip_to_tuple(ip: str) -> Tuple[int, int, int, int]:
2626 return ip_tuple
2727
2828
29- def ip_analyze (ip : str ) -> List [str ]: # pylint: disable=too-many-branches
29+ def ip_analyze (address : str ) -> List [Tuple [ str , int ]]:
3030 """
3131 Analyzes an IP address or range and returns a list of valid IP addresses.
3232
@@ -36,9 +36,15 @@ def ip_analyze(ip: str) -> List[str]: # pylint: disable=too-many-branches
3636 Returns:
3737 list[str]: A list of valid IP addresses.
3838 """
39- if not isinstance (ip , str ):
40- raise TypeError (f"Expected string, got { type (ip ).__name__ } " )
41- ip = ip .replace (" " , "" )
39+ if not isinstance (address , str ):
40+ raise TypeError (f"Expected string, got { type (address ).__name__ } " )
41+ if address .count (":" ) > 1 :
42+ raise ValueError (f"Invalid address format: { address } " )
43+ address = address .replace (" " , "" )
44+ ip , port = address .split (":" ) if ":" in address else (address , - 1 )
45+ if port != - 1 and not (port .isdigit () and (0 <= int (port ) <= 65535 )):
46+ raise ValueError (f"Invalid port number: { port } " )
47+ port = int (port )
4248 if "/" in ip :
4349 try :
4450 ip_addr , mask = ip .split ("/" )
@@ -52,7 +58,7 @@ def ip_analyze(ip: str) -> List[str]: # pylint: disable=too-many-branches
5258 ip_tuple = ip_to_tuple (ip_addr )
5359 ip32 = ip_tuple [0 ] << 24 | ip_tuple [1 ] << 16 | ip_tuple [2 ] << 8 | ip_tuple [3 ]
5460 ip32 |= (1 << (32 - mask )) - 1
55- return [f"{ (ip32 >> 24 ) & 0xFF } .{ (ip32 >> 16 ) & 0xFF } .{ (ip32 >> 8 ) & 0xFF } .{ ip32 & 0xFF } " ]
61+ return [( f"{ (ip32 >> 24 ) & 0xFF } .{ (ip32 >> 16 ) & 0xFF } .{ (ip32 >> 8 ) & 0xFF } .{ ip32 & 0xFF } " , port ) ]
5662 if "-" in ip :
5763 ip_range_tuple = ip .split ("." )
5864 if len (ip_range_tuple ) != 4 :
@@ -75,11 +81,11 @@ def ip_analyze(ip: str) -> List[str]: # pylint: disable=too-many-branches
7581 if ip_count > 65536 :
7682 raise ValueError (f"IP address range too large: { ip_count } addresses" )
7783 return [
78- f"{ a } .{ b } .{ c } .{ d } "
84+ ( f"{ a } .{ b } .{ c } .{ d } " , port )
7985 for a in range (ip_range [0 ][0 ], ip_range [0 ][1 ] + 1 )
8086 for b in range (ip_range [1 ][0 ], ip_range [1 ][1 ] + 1 )
8187 for c in range (ip_range [2 ][0 ], ip_range [2 ][1 ] + 1 )
8288 for d in range (ip_range [3 ][0 ], ip_range [3 ][1 ] + 1 )
8389 ]
8490 ip_tuple = ip_to_tuple (ip )
85- return [f"{ ip_tuple [0 ]} .{ ip_tuple [1 ]} .{ ip_tuple [2 ]} .{ ip_tuple [3 ]} " ]
91+ return [( f"{ ip_tuple [0 ]} .{ ip_tuple [1 ]} .{ ip_tuple [2 ]} .{ ip_tuple [3 ]} " , port ) ]
0 commit comments