24 lines
728 B
Python
24 lines
728 B
Python
from __future__ import annotations
|
|
|
|
import re
|
|
import socket
|
|
|
|
|
|
MAC_RE = re.compile(r"^[0-9A-Fa-f]{2}(?::[0-9A-Fa-f]{2}){5}$")
|
|
|
|
|
|
def normalize_mac(mac: str) -> str:
|
|
value = mac.strip().replace("-", ":")
|
|
if not MAC_RE.match(value):
|
|
raise ValueError(f"Adresse MAC invalide : {mac}")
|
|
return value.upper()
|
|
|
|
|
|
def send_magic_packet(mac: str, broadcast: str = "255.255.255.255", port: int = 9) -> None:
|
|
normalized = normalize_mac(mac)
|
|
raw_mac = bytes.fromhex(normalized.replace(":", ""))
|
|
packet = b"\xff" * 6 + raw_mac * 16
|
|
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
|
sock.sendto(packet, (broadcast, port))
|