最近這幾天,住的地方寬帶經常掉線,十幾分鍾一次,煩死了。後面就想有沒有什麼方法自動撥號,嘗試了windows 的寬帶連接自動撥號 ,一斷開就給你彈出個窗口來,煩死了。後面就想到可以藉助一些工具來實現,比如用vbs 或者python或者其他語言,vbs 個人覺得太難,也不會寫,正好自己會寫一點點python,所以就用python。沒有安裝python環境的可以找個有python的機器上打包一下成爲exe,是可以直接運行的。
代碼:
#encoding:utf8
import os,urllib2,time
class dailog(object):
"""docstring for ClassName"""
def __init__(self,num):
super(dailog, self).__init__()
self.num = num
def check(self):
num=self.num
num1=0
url=['http://www.163.com','http://www.baidu.com','http://www.sina.com.cn']
res=[]
while True:
for x in url:
try:
s=urllib2.urlopen(x)
res.append(s)
except:
res.append(None)
if not any(res):
print ("rasdial dai gb39301 111111") #dai:寬帶連接名稱,gb39301:賬號,111111:密碼
os.popen("rasdial dai /d")
os.popen("rasdial dai gb39301 111111")
num1+=1
else:
print "network is ok"
time.sleep(num)
if num1 ==10:
print "try reconect 10 ago ,error"
break
if __name__ == '__main__':
p=dailog(60)
p.check()