| 
2 | 2 | # for messages from the ESP32 board and prints them  | 
3 | 3 | import socket  | 
4 | 4 | import sys  | 
 | 5 | +import subprocess  | 
 | 6 | +import platform  | 
 | 7 | + | 
 | 8 | +def get_interface_ips():  | 
 | 9 | + """Get all available interface IP addresses"""  | 
 | 10 | + interface_ips = []  | 
 | 11 | + | 
 | 12 | + # Try using system commands to get interface IPs  | 
 | 13 | + system = platform.system().lower()  | 
 | 14 | + | 
 | 15 | + try:  | 
 | 16 | + if system == "darwin" or system == "linux":  | 
 | 17 | + # Use 'ifconfig' on macOS/Linux  | 
 | 18 | + result = subprocess.run(['ifconfig'], capture_output=True, text=True, timeout=5)  | 
 | 19 | + if result.returncode == 0:  | 
 | 20 | + lines = result.stdout.split('\n')  | 
 | 21 | + for line in lines:  | 
 | 22 | + if 'inet ' in line and '127.0.0.1' not in line:  | 
 | 23 | + # Extract IP address from ifconfig output  | 
 | 24 | + parts = line.strip().split()  | 
 | 25 | + for i, part in enumerate(parts):  | 
 | 26 | + if part == 'inet':  | 
 | 27 | + if i + 1 < len(parts):  | 
 | 28 | + ip = parts[i + 1]  | 
 | 29 | + if ip not in interface_ips and ip != '127.0.0.1':  | 
 | 30 | + interface_ips.append(ip)  | 
 | 31 | + break  | 
 | 32 | + elif system == "windows":  | 
 | 33 | + # Use 'ipconfig' on Windows  | 
 | 34 | + result = subprocess.run(['ipconfig'], capture_output=True, text=True, timeout=5)  | 
 | 35 | + if result.returncode == 0:  | 
 | 36 | + lines = result.stdout.split('\n')  | 
 | 37 | + for line in lines:  | 
 | 38 | + if 'IPv4 Address' in line and '127.0.0.1' not in line:  | 
 | 39 | + # 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':  | 
 | 43 | + interface_ips.append(ip)  | 
 | 44 | + except (subprocess.TimeoutExpired, subprocess.SubprocessError, FileNotFoundError):  | 
 | 45 | + print("Error: Failed to get interface IPs using system commands")  | 
 | 46 | + print("Trying fallback methods...")  | 
 | 47 | + | 
 | 48 | + # Fallback: try to get IPs using socket methods  | 
 | 49 | + if not interface_ips:  | 
 | 50 | + try:  | 
 | 51 | + # Get all IP addresses associated with the hostname  | 
 | 52 | + hostname = socket.gethostname()  | 
 | 53 | + ip_list = socket.gethostbyname_ex(hostname)[2]  | 
 | 54 | + for ip in ip_list:  | 
 | 55 | + if ip not in interface_ips and ip != '127.0.0.1':  | 
 | 56 | + interface_ips.append(ip)  | 
 | 57 | + except socket.gaierror:  | 
 | 58 | + print("Error: Failed to get interface IPs using sockets")  | 
 | 59 | + | 
 | 60 | + # Fail if no interfaces found  | 
 | 61 | + if not interface_ips:  | 
 | 62 | + print("Error: No network interfaces found. Please check your network configuration.")  | 
 | 63 | + sys.exit(1)  | 
 | 64 | + | 
 | 65 | + return interface_ips  | 
 | 66 | + | 
 | 67 | +def select_interface(interface_ips):  | 
 | 68 | + """Ask user to select which interface to bind to"""  | 
 | 69 | + if len(interface_ips) == 1:  | 
 | 70 | + print(f"Using interface: {interface_ips[0]}")  | 
 | 71 | + return interface_ips[0]  | 
 | 72 | + | 
 | 73 | + print("Multiple network interfaces detected:")  | 
 | 74 | + for i, ip in enumerate(interface_ips, 1):  | 
 | 75 | + print(f" {i}. {ip}")  | 
 | 76 | + | 
 | 77 | + while True:  | 
 | 78 | + try:  | 
 | 79 | + choice = input(f"Select interface (1-{len(interface_ips)}): ").strip()  | 
 | 80 | + choice_idx = int(choice) - 1  | 
 | 81 | + if 0 <= choice_idx < len(interface_ips):  | 
 | 82 | + selected_ip = interface_ips[choice_idx]  | 
 | 83 | + print(f"Selected interface: {selected_ip}")  | 
 | 84 | + return selected_ip  | 
 | 85 | + else:  | 
 | 86 | + print(f"Please enter a number between 1 and {len(interface_ips)}")  | 
 | 87 | + except ValueError:  | 
 | 88 | + print("Please enter a valid number")  | 
 | 89 | + except KeyboardInterrupt:  | 
 | 90 | + print("\nExiting...")  | 
 | 91 | + sys.exit(1)  | 
5 | 92 | 
 
  | 
6 | 93 | try:  | 
7 | 94 |  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  | 
 | 
10 | 97 |  print("Failed to create socket. Error Code : " + str(msg[0]) + " Message " + msg[1])  | 
11 | 98 |  sys.exit()  | 
12 | 99 | 
 
  | 
 | 100 | +# Get available interfaces and let user choose  | 
 | 101 | +interface_ips = get_interface_ips()  | 
 | 102 | +selected_ip = select_interface(interface_ips)  | 
 | 103 | + | 
13 | 104 | try:  | 
14 |  | - s.bind(("", 3333))  | 
 | 105 | + s.bind((selected_ip, 3333))  | 
15 | 106 | except socket.error as msg:  | 
16 | 107 |  print("Bind failed. Error: " + str(msg[0]) + ": " + msg[1])  | 
17 | 108 |  sys.exit()  | 
18 | 109 | 
 
  | 
19 |  | -print("Server listening")  | 
20 |  | - | 
21 |  | -print("Server listening")  | 
 | 110 | +print(f"Server listening on {selected_ip}:3333")  | 
22 | 111 | 
 
  | 
23 | 112 | while 1:  | 
24 | 113 |  d = s.recvfrom(1024)  | 
 | 
0 commit comments