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