最近这几天,住的地方宽带经常掉线,十几分钟一次,烦死了。后面就想有没有什么方法自动拨号,尝试了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()