def wake_up(request, mac='DC-4A-3E-78-3E-0A'):
MAC = mac
BROADCAST = "192.168.0.255"
if len(MAC) != 17:
raise ValueError("MAC address should be set as form 'XX-XX-XX-XX-XX-XX'")
mac_address = MAC.replace("-", '')
data = ''.join(['FFFFFFFFFFFF', mac_address * 20]) # 構(gòu)造原始數(shù)據(jù)格式
send_data = b''
# 把原始數(shù)據(jù)轉(zhuǎn)換為16進制字節(jié)數(shù)組,
for i in range(0, len(data), 2):
send_data = b''.join([send_data, struct.pack('B', int(data[i: i + 2], 16))])
print(send_data)
# 通過socket廣播出去,為避免失敗,間隔廣播三次
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(send_data, (BROADCAST, 7))
time.sleep(1)
sock.sendto(send_data, (BROADCAST, 7))
time.sleep(1)
sock.sendto(send_data, (BROADCAST, 7))
return HttpResponse()
print("Done")
except Exception as e:
return HttpResponse()
print(e)
到此這篇關(guān)于Python黑魔法遠程控制開機的實例的文章就介紹到這了,更多相關(guān)Python遠程控制開機內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!