Why is the necessary action in the script not performed?

Hello to everyone. That’s the scenario

import socket
import os
import logging
import psutil

WOL_PORT = 9999
INTERFACE_NAME = "My Lan"
INTERFACE_NAME_ARCH = "enp37s0"

logging.basicConfig(format="%(levelname)s: %(asctime)s %(message)s", level=logging.INFO)
logger = logging.getLogger(__name__)

def get_ip_mac_address(interface_name: str) -> tuple:
    ip_addr = mac_addr = None

    for item in psutil.net_if_addrs()[interface_name]:
        addr = item.address

        if "." in addr:
            ip_addr = addr
        elif ("-" in addr or ":" in addr) and "::" not in addr:
            mac_addr = addr.replace(":", "-").upper()

    if not ip_addr or not mac_addr or ip_addr == "127.0.0.1":
        raise "Не удалось получить IP или MAC-адрес сетевого интерфейса"

    return ip_addr, mac_addr

def assemble_wol_packet(mac_address: str) -> str:
    return f'{"FF-" * 6}{(mac_address + "-") * 16}'

def check_is_wol_packet(raw_bytes: bytes, assembled_wol_packet: str) -> int:
    decoded_packet_data = "-".join(f"{byte:02x}" for byte in raw_bytes).upper() + "-"

    if decoded_packet_data == assembled_wol_packet:
        return 1

    return 0

def run_udp_port_listener(port: int, interface_name: str):
    ip_addr, mac_addr = get_ip_mac_address(interface_name)

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_socket.bind((ip_addr, port))
    logger.info(f"Listening on {ip_addr}:{port}")

    assembled_wol_packet = assemble_wol_packet(mac_addr)

    while True:
        data, _ = server_socket.recvfrom(1024)

        is_wol_packet = check_is_wol_packet(data, assembled_wol_packet)

        if is_wol_packet == 1:
            if os.name == "posix":
                os.system("reboot")
            elif os.name == "nt":
                os.system(
                    "shutdown -r -t 0 -f"
                )

if os.name == "posix":
    run_udp_port_listener(WOL_PORT, INTERFACE_NAME_ARCH)
elif os.name == "nt":
    run_udp_port_listener(WOL_PORT, INTERFACE_NAME)

It works great in Windows. That is, the essence is, I start the script, it listens to the port 9999 (WOL). From the phone, I send a package to this port and, accordingly, depending on the OS (this in the scenario), a command is executed. So in Arch I run, netstat -tulpan shows that the port is listening, I send a package from the phone, and nothing happens, although there should be restart. I checked the command separately - os.system (“reboot”) is working.
Tell me what is the problem?

PS. The firewall is not installed, iptables -L everything on ACCEPT.
Sorry for my English, this is not my native language.

What do the logs say? Can you add another logging message to show what is returned by recvfrom()? Also, what lines aren’t running that you expect to run exactly? Are there any exceptions being raised anywhere?

In PyCharm, if I enable all lines on Debug, the script runs, starts listening to the port. I send a packet from the phone, nothing happens, I stop the debug and get this Traceback

/usr/bin/python3.12 /home/blagoyar/Scripts/WoL 9999 [Reboot to Arch].py 
INFO: 2024-11-01 10:57:53,126 Listening on 192.168.0.2:9999
Traceback (most recent call last):
  File "/home/blagoyar/Scripts/WoL 9999 [Reboot to Arch].py", line 68, in <module>
    run_udp_port_listener(WOL_PORT, INTERFACE_NAME_ARCH)
  File "/home/blagoyar/Scripts/WoL 9999 [Reboot to Arch].py", line 54, in run_udp_port_listener
    data, _ = server_socket.recvfrom(1024)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt

Process finished with exit code 130 (interrupted by signal 2:SIGINT)

What do I need to do to get the log from recvfrom()?

When a packet is received, in Arch should trigger this line
os.system(“reboot”)

No exceptions.