wifi 掉线自动重连

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/usr/bin/env python
# coding: utf-8
__author__ = 'Elva'

import os
import time
import sys
#import datetime

from time import localtime, strftime

###每N分钟检查一次网络,如果断开进行重连
#f_handler=open('net_check.log', 'w')
#sys.stdout=f_handler

#ISOFORMAT='%Y-%m-%d %H:%M:%S' #设置输出格式

# isinstance(c,int)
# 
CAO_NI_MA3 = '''
                                         ┏┓   ┏┓    
                                        ┏┛┻━━━┻┗┓  
                                        ┃       ┃  
                                        ┃   ━   ┃  
                                        ┃ ┳┛ ┗┳ ┃  
                                        ┃       ┃  
                                        ┃   ┻   ┃  
                                        ┗━┓  神  ┏━┛  
                                          ┃ 兽  ┃      
                                      ┏━━━┛ 佑 ┃      
                                     ┏┛          护  ┃      
                                     ┗┓              ┃      
                                      ┗┓┏┳━┓┏┏┛      
                                       ┣┣┃ ┣┣┃        
                                       ┗┻┛ ┗┻┛        '''

  
CAO_NI_MA = '''
       ┏┓   ┏┓    
      ┏┛┻━━━┻┗┓  
      ┃       ┃  
      ┃   ━   ┃  
      ┃ ┳┛ ┗┳ ┃  
      ┃       ┃  
      ┃   ┻   ┃  
      ┗━┓      ┏━┛  
        ┃ 草 ┃      
    ┏━━━┛ 尼 ┃      
   ┏┛          玛  ┃      
   ┗┓              ┃      
    ┗┓┏┳━┓┏┏┛      
     ┣┣┃ ┣┣┃        
     ┗┻┛ ┗┻┛        '''
  
def incCount():
	'''计数+1'''
	fh = open("rc_count.txt",'r+')
	count_ = fh.read()
	count_ = int(count_)
	count_ = count_ + 1
	#print count_
	fh.seek(0)
	fh.write(str(count_))
	fh.close()

def log(text):
	''' 记录日志'''
	#global ISOFORMAT
	#now = datetime.datetime.now()
	nowStr = strftime("%Y-%m-%d %H:%M:%S", localtime());
	msg = nowStr + " : " + text
	print(msg)
	fh = open("_netcheck.log",'a')
	fh.write(msg + "\n")
	fh.close()
log("********************【--GOOD_LUCK--】********************")
log(CAO_NI_MA3)
log("********************【--GOOD_LUCK--】********************")
#log("qcc")
	
#等待多少秒
def wait(s):
	''' 等待S秒'''
	#print("---------------------SLEEP(%ss)--------------------" %s)
	if s <= 0:
		return
	i = s*2-1;
	while i > 0:
		i=i-1
		time.sleep(0.5)
		print '.',
	time.sleep(0.5)
	print '.'
	#print("---------------------SLEEP(%ss)-------------------" %s)
#wait(3)
#检查网络计数
check_net_count = 0
#重连计数
reconnect_count = 0
#域名索引
domain_index = 0
def check_net():
	'''检查网络'''
	print("");
	global check_net_count
	global reconnect_count
	global domain_index
	check_net_count=check_net_count+1
	log("---------------------CHECK_NET_STA(%d)---------------------" %check_net_count)
	#ping_list =["www.qq.com","www.163.com","www.sohu.com","www.sina.com","www.oschina.net","www.hao123.com","www.taobao.com","www.autohome.com.cn","www.youku.com","www.jd.com","www.iteye.com","www.ctrip.com"]
	ping_list = ["www.baidu.com","www.weibo.com","www.qq.com","www.163.com","www.sohu.com","www.sina.com","www.oschina.net","www.hao123.com","www.taobao.com","www.autohome.com.cn","www.youku.com","www.jd.com","www.1688.com","www.iteye.com","mail.126.com","www.ctrip.com","www.360.cn","www.iciba.com","www.wiz.cn","www.csdn.net","www.net.cn","mail.163.com","www.weiyun.com","www.suning.com","www.ganji.com","www.xcar.com.cn","mail.qq.com","www.ifeng.com","www.yhd.com","www.liepin.com","www.xunlei.com","www.weibo.cn","www.xxsy.net","www.dianping.com","www.mop.com","www.126.com","www.zol.com.cn","www.qunar.com","www.alipay.com","www.17173.com","www.4399.com","www.fang.com","www.baihe.com","www.51job.com","www.letv.com","www.iqiyi.com","www.58.com","www.amazon.cn","www.gome.com.cn"]
	#ping_list =["www.baidu.com","www.qq.com","www.163.com","www.sohu.com","www.sina.com","www.oschina.net"]
	ping_str = "ping -n 1 " + ping_list[(domain_index)%(len(ping_list))]
	log("INFO:PING:["+ ping_list[(domain_index)%(len(ping_list))] +"]...")
	domain_index = domain_index + 1;
	#ret = os.system("ping www.qq.com")
	ret = os.system(ping_str)
	log("INFO:PING STATUS_CODE:%d" %ret)
	if ret:
		#try again
		log("WARN:NET MAY DOWN,try2 next domain!!!")
		#time.sleep(5)#暂停N秒
		wait(3)
		ping_str = "ping " + ping_list[(domain_index)%(len(ping_list))]
		log("INFO:PING2:["+ ping_list[(domain_index)%(len(ping_list))] +"]...")
		domain_index = domain_index + 1; # domain_index+=1
		ret = os.system(ping_str)
		log("INFO:PING2 STATUS_CODE:%d" %ret)
		if ret:
			#try again
			log("WARN:NET MAY DOWN,try3 next domain!!!")
			#time.sleep(5)#暂停N秒
			wait(3)
			ping_str = "ping " + ping_list[(domain_index)%(len(ping_list))]
			log("INFO:PING3:["+ ping_list[(domain_index)%(len(ping_list))] +"]...")
			domain_index = domain_index + 1; # domain_index+=1
			ret = os.system(ping_str)
			log("INFO:PING3 STATUS_CODE:%d" %ret)
	if not ret:
		log("INFO:NET IS OK (RC=%d)!!" %reconnect_count)
		log("---------------------CHECK_NET_END(%d)---------------------\n" %check_net_count)
	else:
		print(CAO_NI_MA)
		log("FUCK:NET IS DOWN!")
		
		reconnect_count = reconnect_count + 1
		incCount()
		log("INFO:尝试断开重连(%d)..." %reconnect_count);
		#先断开连接
		log("INFO:尝试断开连接...");
		netsh_ret = os.system("netsh wlan disconnect")
		wait(3)
		log("INFO:尝试重新连接...");
		netsh_ret = os.system("netsh wlan connect name=TP-LINK_490A")
		#wait(2)
		log("INFO:NETSH_RET STATUS_CODE:%d" %netsh_ret)
		log("---------------------CHECK_NET_END(%d)---------------------\n" %check_net_count)

#incCount()
while True:
	check_net()
	time.sleep(45)#每五分钟PING一次
	#time.sleep(10)#每五分钟PING一次