Python wifi掉线重连接

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python wifi掉线重连接相关的知识,希望对你有一定的参考价值。

     原理很简单,通过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
连接状态

 

























以上是关于Python wifi掉线重连接的主要内容,如果未能解决你的问题,请参考以下文章

电脑WIFI网络用一会就掉线,只有重启或者待机重新打开之后才又自动连接上去

ubuntu16.04 无法wifi链接一段时间掉线且无法再连接

360wifi: 手机锁屏360wifi掉线的解决方法

WiFi 掉线时未使用 CommandTimeout

家里网络先换掉线 一天掉20次以上 过个几分钟又自动重连了 宽带拨号有时候会说651错误 调制解调器啥的

树莓派ssh总掉线