提前条件能通过adb命令控制WiFi开关
import os import time class Wifi(): def __init__(self,count): self.count = count # 开启wifi的方法 def openWifi(self): cmd = 'adb shell svc wifi enable' os.popen(cmd) time.sleep(10) # 关闭wifi的方法 def closeWifi(self): cmd = 'adb shell svc wifi disable' os.popen(cmd) time.sleep(10) #获取wifi当前开关状态 def getWifiStatue(self): cmd='adb shell settings get global wifi_on' f = os.popen(cmd) str = f.readlines() if( str == ['1 ']): print("wifi成功开启") elif(str == ['0 ']): print("wifi成功关闭") else: print('wifi出错了') #控制wifi循环的方法 def controlWifi(self): i = 1 while (self.count >0): print("第 %d 次执行关闭Wi-Fi操作:" % i) self.closeWifi() self.getWifiStatue() print("第 %d 次执行开启Wi-Fi操作:" % i) self.openWifi() self.getWifiStatue() i = i +1 self.count = self.count - 1 else: print("总共执行wifi开关次数 %d" % count) time.sleep(18000) if __name__ == '__main__': #控制Wi-Fi开关执行次数 count=int(input("请输入执行wifi开关次数:")) app = Wifi(count) app.controlWifi()