Python自动切换wifi

前言

最近在打游戏的时候经常会遇到断网的情况,每次都需要切出去切换wifi,很烦人,所以就写了个用Python自动切换可用的wifi来减少困扰。

几个系统命令

这次我们是使用python中的os模块来模拟命令行来执行命令切换wifi,所以在写程序之前,可以先了解一下几个命令。

查看当前wifi:netsh wlan show interfaces

查看所有wifi:netsh wlan show profiles

连接wifi:netsh wlan connect name="wifi名称"

思路

这次我们写的程序的主要思路如下:

1.获取当前wifi
2.测试当前wifi能否ping通百度
3.如果能ping通则等待5s后继续测试
4.如果ping不通则在能够连接的wifi中随机选择一个来连接

代码

获取当前wifi

import os
import subprocess

def get_current_wifi():
    cmd = 'netsh wlan show interfaces'
    p = subprocess.Popen(cmd,
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        shell=True)
    ret = p.stdout.read()
    index = ret.find("SSID")
    if index > 0:
        return ret[index:].split(':')[1].split('\r\n')[0].strip()
    else:
        return None

这里我们使用subprocess.Popen函数来模拟执行命令行命令,并通过read()方法得到命令行的结果,接着对结果进行分析可以得到当前的wifi。

测试能否ping通

def check_ping(ip, count=1, timeout=1000):
    cmd = 'ping -n %d -w %d %s > NUL' % (count, timeout, ip)
    res = os.system(cmd)
    return 'ok' if res == 0 else 'failed'

这里我们首先构建了一个cmd命令来ping我们自己传递过来的ip地址,然后使用os.system()函数执行该命令,如果返回值为0则ping通,否则失败。

自动切换wifi

import random

def auto_switch_wifi(wifiList):
    wifi = random.choice(wifiList)
    cmd = 'netsh wlan connect name={}".format(wifi)
    res = os.system(cmd)
    return 'ok' if res == 0 else 'failed'

在auto_switch_wifi()函数中,我们接收一个可用的wifi列表,然后再列表中随机选择一个wifi进行切换,如果成功则返回ok。

到这里我们的几大基本模块已经写完了,下面上完整代码。

# _*_ coding:utf-8 _*_
import os
import time
import subprocess
import random


def check_ping(ip, count=1, timeout=1000):
    cmd = 'ping -n %d -w %d %s > NUL' % (count, timeout, ip)
    # 通过os.system()方法执行命令
    response = os.system(cmd)
    return 'ok' if response == 0 else 'failed'


def get_current_wifi():
    cmd = 'netsh wlan show interfaces'
    p = subprocess.Popen(cmd,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         shell=True)
    ret = p.stdout.read()
    index = ret.find('SSID')
    if index > 0:
        return ret[index:].split(':')[1].split('\r\n')[0].strip()


def auto_switch_wifi(wifiList):
    wifi = random.choice(wifiList)
    cmd = 'netsh wlan connect name="%s"' % wifi
    res = os.system(cmd)
    return 'ok' if res == 0 else 'failed'



def main():
    # 百度ip
    ipTest = '61.135.169.121'
    # 可以切换的wifi
    wifiList = ['HUAWEI-5DD8']
    while True:
        current_wifi = get_current_wifi()
        print "当前的wifi为:", current_wifi
        if check_ping(ipTest, 2) != 'ok':
            print "联网失败,正在切换wifi"
            if auto_switch_wifi(wifiList) == 'ok':
                print "切换成功"
                print "-" * 40
            else:
                continue
            time.sleep(5)
        else:
            print "可以成功联网"
            print '-' * 40
            time.sleep(5)


if __name__ == "__main__":
    main()

总结

人生苦短,我用python!代码还有可以完善的地方,如果想要扩展更多功能的童鞋可以自己探索哈!

猜你喜欢

转载自blog.csdn.net/qq_34377830/article/details/82497457
今日推荐