python 处理WiFi 相关

1、连接指定WiFi

def connectWifi(ssid, passwd="", mode="", timeout=20):
    '''
    连接指定WIFI。
    mode取值:WEP, WPA, WPA2, WPAPSK, WPA2PSK, UNKWON
    '''
    global global_logger

    pywifi.set_loglevel(logging.ERROR)
    wifi = pywifi.PyWiFi()
    ifaces = wifi.interfaces()
    if len(ifaces) == 0:
        global_logger.warning("Can not find wifi interface.")
        return False
    iface = ifaces[0]

    print("Disconnect current wifi.")
    iface.disconnect()  # 先断开网络。
    time.sleep(3)

    setmode = const.AKM_TYPE_UNKNOWN
    if mode == "WPA":
        setmode = const.AKM_TYPE_WPA
    elif mode == "WPAPSK":
        setmode = const.AKM_TYPE_WPAPSK
    elif mode == "WPA2":
        setmode = const.AKM_TYPE_WPA2
    elif mode == "WPA2PSK":
        setmode = const.AKM_TYPE_WPA2PSK
 profile = pywifi.Profile()
    profile.ssid = ssid  # 如: RoboVac_330G_400059
    profile.key  = passwd
    profile.auth = const.AUTH_ALG_OPEN
    profile.akm.append(setmode)  # 无加密
    # profile.cipher = const.CIPHER_TYPE_UNKNOWN

    tmp_profile = iface.add_network_profile(profile)
    global_logger.info("Connecting to wifi " + ssid)
    iface.connect(tmp_profile)

    tmout = 0
    while (tmout < timeout):
        if iface.status() == const.IFACE_CONNECTED:
            #global_logger.warning("Connect to wifi success:" + iface.scan_results()[0].ssid)
            global_logger.info("Connect to wifi {} SUCCESS!".format(ssid))
            return True
        else:
            time.sleep(5)
            tmout = tmout + 5

    print("Connect new wifi timeout.")
    return False

2、连接智能设备WiFi

def connectMachineWifi(ssid, timeout=20):
    '''
    连接到智能设备WiFi。用于配网测试。
    :param ssid: 智能设备的WIFI名称。
    智能设备的WIFI不涉及密码。

    返回:连接WIFI成功返回True,失败返回False。
    '''
    global global_logger

    pywifi.set_loglevel(logging.ERROR)
    wifi = pywifi.PyWiFi()
    ifaces = wifi.interfaces()
    if len(ifaces) == 0:
        print("Can not find wifi interface.")
        return False
    iface = ifaces[0]

    print("Disconnect current wifi.")
    iface.disconnect()  #先断开网络。
    time.sleep(5)

    profile = pywifi.Profile()
    profile.ssid   = ssid  
    profile.auth   = const.AUTH_ALG_OPEN
    profile.akm.append(const.AKM_TYPE_NONE)  #无加密
    # profile.cipher = const.CIPHER_TYPE_NONE  #无加密

    tmp_profile = iface.add_network_profile(profile)
    global_logger.info("Connecting to wifi {}".format(ssid))
    iface.connect(tmp_profile)
    tmout = 0
    while (tmout < timeout):
        if iface.status() == const.IFACE_CONNECTED:
            time.sleep(2)
            # global_logger.info("Connect to wifi success:{}".format(iface.scan_results()[0].ssid.encode(encoding="utf-8", errors="replace")))
            global_logger.info("Connect to {} SUCCESS!".format(ssid))
            return True
        else:
            time.sleep(5)
            tmout = tmout + 5

    print("Connect new wifi TIMEOUT.")
    return False

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值