原理很简单,通过python执行dos命令 : ping 和 netsh

需要用到os和time模块

代码如下:

>>> import os
>>> print 'wifi掉线'
wifi掉线
>>> import time
>>> while True:
...     status = os.system("ping -c 1 www.baidu.com")
...     if status == 1:
...         print 'wifi掉线'
...         os.system('netsh wlan connect name=DSGY2')
...     else:
...         print '连接状态'
...     time.sleep(180)

大家使用时将name=XX ,名字改一下,在我的机子上测试通过,该脚本3分钟检测一次是否掉线

如果你的wifi在手动连接都连不上,那这段脚本就没用了

------------------------------------------------------------------------------------------------------------------------

Pinging www.a.shifen.com [14.215.177.37] with 32 bytes of data:
Reply from 14.215.177.37: bytes=32 time=24ms TTL=54
Reply from 14.215.177.37: bytes=32 time=63ms TTL=54
Reply from 14.215.177.37: bytes=32 time=32ms TTL=54
Reply from 14.215.177.37: bytes=32 time=26ms TTL=54

Ping statistics for 14.215.177.37:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 24ms, Maximum = 63ms, Average = 36ms
连接状态

Pinging www.a.shifen.com [180.97.33.108] with 32 bytes of data:
Reply from 180.97.33.108: bytes=32 time=1676ms TTL=54
Reply from 180.97.33.108: bytes=32 time=544ms TTL=54
Reply from 180.97.33.108: bytes=32 time=2033ms TTL=54
Reply from 180.97.33.108: bytes=32 time=291ms TTL=54

Ping statistics for 180.97.33.108:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 291ms, Maximum = 2033ms, Average = 1136ms
连接状态

Pinging www.a.shifen.com [180.97.33.107] with 32 bytes of data:
Reply from 180.97.33.107: bytes=32 time=126ms TTL=54
Reply from 180.97.33.107: bytes=32 time=294ms TTL=54
Reply from 180.97.33.107: bytes=32 time=57ms TTL=54
Reply from 180.97.33.107: bytes=32 time=131ms TTL=54

Ping statistics for 180.97.33.107:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 57ms, Maximum = 294ms, Average = 152ms
连接状态

----------------------------------------------------------------------------------------------------------------

昨天没有把代码保存,也没有碰到掉线的情况

#-*-coding:utf-8-*-
import os
import time
while True:
    status = os.system('ping -c 1 www.baidu.com')
    if status == 1:
        print u'wifi掉线'
        os.system('netsh wlan connect name=haha')
    else:
        print u'连接状态'
    time.sleep(30)

运行情况

Pinging www.a.shifen.com [180.97.33.108] with 32 bytes of data:
Reply from 180.97.33.108: bytes=32 time=21ms TTL=56
Reply from 180.97.33.108: bytes=32 time=22ms TTL=56
Reply from 180.97.33.108: bytes=32 time=45ms TTL=56
Reply from 180.97.33.108: bytes=32 time=25ms TTL=56

Ping statistics for 180.97.33.108:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 21ms, Maximum = 45ms, Average = 28ms
连接状态
Ping request could not find host www.baidu.com. Please check the name and try again.
wifi掉线
Connection request was completed successfully.

Pinging www.a.shifen.com [180.97.33.108] with 32 bytes of data:
Reply from 180.97.33.108: bytes=32 time=26ms TTL=56
Reply from 180.97.33.108: bytes=32 time=27ms TTL=56
Reply from 180.97.33.108: bytes=32 time=27ms TTL=56
Reply from 180.97.33.108: bytes=32 time=28ms TTL=56

Ping statistics for 180.97.33.108:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 26ms, Maximum = 28ms, Average = 27ms
连接状态

---注意:在管理员权限下运行脚本

Python wifi掉线重连接的更多相关文章

  1. WCF心跳判断服务端及客户端是否掉线并实现重连接

    WCF心跳判断服务端及客户端是否掉线并实现重连接 本篇文章将通过一个实例实现对WCF中针对服务端以及客户端是否掉线进行判断:若掉线时服务器或客户端又在线时将实现自动重连:将通过WCF的双工知识以及相应 ...

  2. wind10系统 Atheros AR9271 Wireless Network Adapter USBwifi无线网卡的驱动安装解决无法搜索wifi信号,连接wifi信号无法上网的问题

    一.解决无法搜索wifi信号的问题 卸载掉之前的驱动,上网下载其他的驱动程序安装. http://drivers.mydrivers.com/drivers/463_185289.htm 二.安装完后 ...

  3. python之requests urllib3 连接池

    0.目录 1.参考 2. pool_connections 默认值为10,一个站点主机host对应一个pool (4)分析 host A>>host B>>host A pag ...

  4. [Spark][Python]DataFrame的左右连接例子

    [Spark][Python]DataFrame的左右连接例子 $ hdfs dfs -cat people.json {"name":"Alice",&quo ...

  5. Atitit 列出wifi热点以及连接

    Atitit 列出wifi热点以及连接 配置命令 >netsh wlan  /?1 显示已经有的配置netsh wlan show profiles1 C:\Users\Administrato ...

  6. python中实现mysql连接池

    python中实现mysql连接池 import pymysql from DBUtils.PooledDB import PooledDB MYSQL_HOST = 'localhost' USER ...

  7. Python和mysql的连接

    python与mysql的连接: 说明:前提是已近安装了mysql以及可视化工具(本人装的是Navicat) 1.在cmd下下载Python的第三方数据库包:pip install pymysql: ...

  8. python中列表元素连接方法join用法实例

    python中列表元素连接方法join用法实例 这篇文章主要介绍了python中列表元素连接方法join用法,实例分析了Python中join方法的使用技巧,非常具有实用价值,分享给大家供大家参考. ...

  9. ws & websocket & 掉线重连

    ws & websocket & 掉线重连 reconnecting websocket https://github.com/joewalnes/reconnecting-webso ...

随机推荐

  1. BackgroundWorker组件使用总结

    首先在窗体拖入一个BackgroundWorker组件,根据功能需要设置BackgroundWorker的属性 WorkerSupportsCancellation = true; 允许取消后台正在执 ...

  2. Ant 随想

    Ant是一种基于Java的build工具 面向任务构建,属性与于shell脚本中命令功能类型. <?xml version="1.0"?> <project na ...

  3. Visual Studio 2013 IIS Explorer 停止调试继续访问站点

    升级到2013后,在做调试的时候默认调试服务器是 IIS Explorer,当终止调试的时候再次访问调试站点时已经无法访问了.此时想预览一下感觉很不方便. 为了能够预览可以参考一下配置: Tools  ...

  4. MFC圆角背景移动边角底色毛刺解决方案

    CRect rc; Graphics graphics(pDC->m_hDC); GetClientRect(&rc); CRgn m_rgn; if (m_pBgImage) { gr ...

  5. .NET软件开发资源

    .NET软件开发资源 最近建了一个.NET软件开发资源的360网盘共享群,把收集的一些.NET软件开发资源分享给大家,也欢迎大家把好的东东分享一下. 资源主要有:开发工具.控件资源.书籍教程.网页设计 ...

  6. ASP.NET MVC中使用Unity进行依赖注入的三种方式

    在ASP.NET MVC中使用Unity进行依赖注入的三种方式 2013-12-15 21:07 by 小白哥哥, 146 阅读, 0 评论, 收藏, 编辑 在ASP.NET MVC4中,为了在解开C ...

  7. UVA 10392 (13.07.28)

    Problem F: Factoring Large Numbers One of the central ideas behind much cryptography is that factori ...

  8. RSA加密解密与签名验证

    关于RSACryption帮助类定义见RSACryption 一.加密与解密 //定义明文和密文变量 string plaintext = "天道酬勤,厚德载物!"; string ...

  9. C#.Net获取Mac等PC信息

    using System.Management; using System.Net; public class PCInfoHelper { /// <summary> /// 操作系统的 ...

  10. 哞哞快的 C# 高斯模糊实现(续)

    昨天刚写了<哞哞快的 C# 高斯模糊实现>,里边提到了用原作者的方法实现对图像快速的高斯模糊处理,说实话,我没看懂,主要是没看懂原理,怎么就“把图片给处理了”,大概是调用了 GDIPlus ...