python 自动重连wifi windows的方法
如下所示:
#coding=utf-8 importurllib2 importurllib fromcookielibimportCookieJar importos importre importtime classConnectWeb(object): def__init__(self): self.cookiejarinmemory=CookieJar() self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookiejarinmemory)) urllib2.install_opener(self.opener) self.username="" self.password="" defconnect_baidu(self):#检测目前是否联网 try: urllib2.urlopen("http://www.baidu.com",timeout=2) return1 except: return0 deflogin(self):#模拟上网验证验证网页几乎都是不同的,下面附上我们学校的,form表单自己根据情况填,用chrome很容易得到post的url和表单 try: post_url="" form={"action":"login","username":self.username,"password":self.password,"ac_id":4, "user_ip":"","nas_ip":"","user_mac":"","save_me":1,"ajax":1} fm1=urllib.urlencode(form) page=urllib2.urlopen(post_url,fm1).read() exceptExceptionase: self.disconnect() time.sleep(1) self.connect_wifi() defdisconnect(self): #断开wifi os.system("netshwlandisconnect") defwifis_nearby(self): #查询附近wifi p=os.popen("netshwlanshowall") content=p.read().decode("GB2312","ignore") temp=re.findall(u"(SSID.*\n.*Networktype.*\n.*\u8eab\u4efd\u9a8c\u8bc1.*\n.*\u52a0\u5bc6.*\n.*BSSID.*\n)", content) result=[] foriintemp: name=re.findall(u"SSID.*:(.*)\n",i)[0].replace("","") result.append(name) returnresult defconnect_wifi(self,name=None):#连接wifi os.system("netshwlanconnectname=%s"%name) defchecking(self): #一直检测是否有断网,如果断网则重新连接 while1: try: ifnotself.connect_baidu(): self.login() except: pass time.sleep(10) if__name__=="__main__": test=ConnectWeb() test.login()
以上这篇python自动重连wifiwindows的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。