environment:
win10
python3.12
Problem description:
How to write a script to wake up a sleeping computer using python?
Solution:
1. Wake up a sleeping computer usually not directly implemented through programming, but rely on features provided by the hardware and operating system. For Windows systems, a computer in sleep or hibernation mode can be awakened by sending a special network packet (Wake-on-LAN, WoL), provided that the computer supports WoL function and has been enabled in the BIOS/UEFI settings.
2. To use Python to write a program to wake up a sleeping computer, you need to know the MAC address of the target machine and make sure that the target machine and the machine sending the command are in the same LAN, or that the router supports forwarding WoL packets.
3. Here is a simple Python script name for sending Wake-on-LAN magic packets:
import socket def create_magic_packet(macaddress): """ Create aWake-on-LANMagic data package。 parameter: macaddress (str): Target deviceMACaddress,The format is'XX:XX:XX:XX:XX:XX'。 return: bytes: Magic packets as byte sequences。 """ # Remove possible delimiters and convert them to uppercase macaddress = ().replace('-', '').replace(':', '') if len(macaddress) != 12: raise ValueError("MAC address must be 12 characters long") # Convert MAC address to byte sequence mac_bytes = (macaddress) # Create magic packet: 6 FF bytes plus 16 repeated MAC addresses packet = b'\xff' * 6 + mac_bytes * 16 return packet def send_magic_packet(macaddress, ip="255.255.255.255", port=9): """ sendWake-on-LANMagic packet to the specifiedMACaddress。 parameter: macaddress (str): Target deviceMACaddress。 ip (str): broadcastIPaddress,Default is255.255.255.255。 port (int): Port number,Default is9。 """ packet = create_magic_packet(macaddress) with (socket.AF_INET, socket.SOCK_DGRAM) as sock: (socket.SOL_SOCKET, socket.SO_BROADCAST, 1) (packet, (ip, port)) print(f"已send魔幻数据包到 {macaddress}.") # Example usageif __name__ == "__main__": MAC_ADDRESS = "00-1A-2B-3C-4D-5E" # Replace with the actual MAC address of the target machine send_magic_packet(MAC_ADDRESS)
4. Please modify the MAC_ADDRESS variable according to the actual situation to match the MAC address of the computer you want to wake up.
Note that in order for Wake-on-LAN to work, in addition to the above code, the following conditions need to be met:
- The target computer's network adapter must support Wake-on-LAN and enable this feature in BIOS/UEFI.
- In the operating system's power management settings, allow the network adapter to wake up the computer.
- If your router does not broadcast outside the subnet, you need to make sure that the computer sending the WoL packet is on the same subnet as the target computer, or configure the router to allow WoL packets to pass.
- If you are using a wireless network adapter, please note that not all wireless network cards support Wake-on-LAN function.
5. Write a bat at the end
python
6. Run bat to wake up the corresponding computer
This is the end of this article about writing a script to wake up a sleep computer in Python. For more related content on python wake up a sleep computer, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!