5
5
import subprocess
6
6
import platform
7
7
8
+
8
9
def get_interface_ips ():
9
10
"""Get all available interface IP addresses"""
10
11
interface_ips = []
@@ -15,31 +16,31 @@ def get_interface_ips():
15
16
try :
16
17
if system == "darwin" or system == "linux" :
17
18
# 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 )
19
20
if result .returncode == 0 :
20
- lines = result .stdout .split (' \n ' )
21
+ lines = result .stdout .split (" \n " )
21
22
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 :
23
24
# Extract IP address from ifconfig output
24
25
parts = line .strip ().split ()
25
26
for i , part in enumerate (parts ):
26
- if part == ' inet' :
27
+ if part == " inet" :
27
28
if i + 1 < len (parts ):
28
29
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" :
30
31
interface_ips .append (ip )
31
32
break
32
33
elif system == "windows" :
33
34
# 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 )
35
36
if result .returncode == 0 :
36
- lines = result .stdout .split (' \n ' )
37
+ lines = result .stdout .split (" \n " )
37
38
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 :
39
40
# 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" :
43
44
interface_ips .append (ip )
44
45
except (subprocess .TimeoutExpired , subprocess .SubprocessError , FileNotFoundError ):
45
46
print ("Error: Failed to get interface IPs using system commands" )
@@ -52,7 +53,7 @@ def get_interface_ips():
52
53
hostname = socket .gethostname ()
53
54
ip_list = socket .gethostbyname_ex (hostname )[2 ]
54
55
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" :
56
57
interface_ips .append (ip )
57
58
except socket .gaierror :
58
59
print ("Error: Failed to get interface IPs using sockets" )
@@ -64,6 +65,7 @@ def get_interface_ips():
64
65
65
66
return interface_ips
66
67
68
+
67
69
def select_interface (interface_ips ):
68
70
"""Ask user to select which interface to bind to"""
69
71
if len (interface_ips ) == 1 :
@@ -90,6 +92,7 @@ def select_interface(interface_ips):
90
92
print ("\n Exiting..." )
91
93
sys .exit (1 )
92
94
95
+
93
96
try :
94
97
s = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
95
98
s .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
0 commit comments