55import subprocess
66import platform
77
8+
89def get_interface_ips ():
910 """Get all available interface IP addresses"""
1011 interface_ips = []
@@ -15,31 +16,31 @@ def get_interface_ips():
1516 try :
1617 if system == "darwin" or system == "linux" :
1718 # Use 'ifconfig' on macOS/Linux
18- result = subprocess .run ([' ifconfig' ], capture_output = True , text = True , timeout = 5 )
19+ result = subprocess .run ([" ifconfig" ], capture_output = True , text = True , timeout = 5 )
1920 if result .returncode == 0 :
20- lines = result .stdout .split (' \n ' )
21+ lines = result .stdout .split (" \n " )
2122 for line in lines :
22- if ' inet ' in line and ' 127.0.0.1' not in line :
23+ if " inet " in line and " 127.0.0.1" not in line :
2324 # Extract IP address from ifconfig output
2425 parts = line .strip ().split ()
2526 for i , part in enumerate (parts ):
26- if part == ' inet' :
27+ if part == " inet" :
2728 if i + 1 < len (parts ):
2829 ip = parts [i + 1 ]
29- if ip not in interface_ips and ip != ' 127.0.0.1' :
30+ if ip not in interface_ips and ip != " 127.0.0.1" :
3031 interface_ips .append (ip )
3132 break
3233 elif system == "windows" :
3334 # Use 'ipconfig' on Windows
34- result = subprocess .run ([' ipconfig' ], capture_output = True , text = True , timeout = 5 )
35+ result = subprocess .run ([" ipconfig" ], capture_output = True , text = True , timeout = 5 )
3536 if result .returncode == 0 :
36- lines = result .stdout .split (' \n ' )
37+ lines = result .stdout .split (" \n " )
3738 for line in lines :
38- if ' IPv4 Address' in line and ' 127.0.0.1' not in line :
39+ if " IPv4 Address" in line and " 127.0.0.1" not in line :
3940 # Extract IP address from ipconfig output
40- if ':' in line :
41- ip = line .split (':' )[1 ].strip ()
42- if ip not in interface_ips and ip != ' 127.0.0.1' :
41+ if ":" in line :
42+ ip = line .split (":" )[1 ].strip ()
43+ if ip not in interface_ips and ip != " 127.0.0.1" :
4344 interface_ips .append (ip )
4445 except (subprocess .TimeoutExpired , subprocess .SubprocessError , FileNotFoundError ):
4546 print ("Error: Failed to get interface IPs using system commands" )
@@ -52,7 +53,7 @@ def get_interface_ips():
5253 hostname = socket .gethostname ()
5354 ip_list = socket .gethostbyname_ex (hostname )[2 ]
5455 for ip in ip_list :
55- if ip not in interface_ips and ip != ' 127.0.0.1' :
56+ if ip not in interface_ips and ip != " 127.0.0.1" :
5657 interface_ips .append (ip )
5758 except socket .gaierror :
5859 print ("Error: Failed to get interface IPs using sockets" )
@@ -64,6 +65,7 @@ def get_interface_ips():
6465
6566 return interface_ips
6667
68+
6769def select_interface (interface_ips ):
6870 """Ask user to select which interface to bind to"""
6971 if len (interface_ips ) == 1 :
@@ -90,6 +92,7 @@ def select_interface(interface_ips):
9092 print ("\n Exiting..." )
9193 sys .exit (1 )
9294
95+
9396try :
9497 s = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
9598 s .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
0 commit comments