forked from kun945/weixind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wol.py
35 lines (23 loc) · 856 Bytes
/
wol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python
# encoding: utf-8
import socket
import struct
def wake_on_lan(mac, broadcast_ip=None):
'''Broadcast magic data to wakeup your pc.'''
if len(mac) == 17:
mac = mac.replace(mac[2], '')
if len(mac) != 12:
raise ValueError('Invalid MAC address')
data = 'FFFFFFFFFFFF%s' %(mac*16)
magic_data = ''
for i in range(0, len(data), 2):
magic_data = ''.join([magic_data, struct.pack('B', int(data[i:i+2], 16))])
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
if not broadcast_ip: broadcast_ip = '255.255.255.255'
#sock.sendto(magic_data, (broadcast_ip, 0))
sock.sendto(magic_data, (broadcast_ip, 7))
sock.sendto(magic_data, (broadcast_ip, 9))
return True
if __name__ == '__main__':
pass