宿舍火星wifi经常掉,然后要重启,于是用Python写了一个脚本,用来远程控制火星wifi的重启
思路:
01.使用socket通讯
02.在wifi主机(开wifi的电脑)上运行客户端,控制机运行服务端
03.当想要重启wifi时候,通过控制机向wifi主机发送字符‘1’
04.wifi主机收到字符后,当判断为‘1’时,Python调用cmd杀掉火星wifi进程,然后等一会后再启动进程
05.完成重启后向控制机发送一条消息显示重启成功
控制机脚本:
1 import socket 2 import time 3 sock = socket.socket() 4 sock.connect(('192.168.191.1',4331)) 5 sock.send('1') 6 msg = sock.recv(1024) 7 print msg 8 time.sleep(3)
wifi主机脚本:
1 import os 2 import socket 3 import time 4 print 'import over,application start:' 5 sock = socket.socket() 6 sock.bind(('localhost',4321)) 7 sock.listen(5) 8 print 'listening...' 9 while True: 10 try: 11 client,addr = sock.accept() 12 print 'connection address is :',addr 13 client.settimeout(5) 14 buf = client.recv(10) 15 print 'receive msg:',buf 16 if buf == '1': 17 os.system("taskkill /f /im winword.exe") 18 print 'has killed marswifi!' 19 time.sleep(3) 20 os.system('start winword.exe') 21 time.sleep(5) 22 print 'has restarted marswifi.' 23 client.send('restart over.') 24 else: 25 print 'no command,sir' 26 client.send('not useful command,sir') 27 except socket.timeout: 28 print 'time out' 29 time.sleep(1)
经过实验,可以远程重启火星wifi
然而,并没什么卵用,wifi掉了就没网发指令了[尴尬.jpg],不过感觉挺有意思的,权当作练习socket