python如何實(shí)現(xiàn)wifi自動(dòng)連接,解決電腦wifi經(jīng)常斷開問題
更新時(shí)間:2023年06月03日 14:57:22 作者:Achen's
這篇文章主要介紹了python實(shí)現(xiàn)wifi自動(dòng)連接,解決電腦wifi經(jīng)常斷開的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
python實(shí)現(xiàn)wifi自動(dòng)連接,解決電腦wifi經(jīng)常斷開
安裝pywifi模塊
pywifi依賴comtypes,不安裝會報(bào)錯(cuò)
pip install pywifi pip install comtypes
代碼
import pywifi from pywifi import const import time def is_connected(wifi_inter): if wifi_inter.status() in [const.IFACE_CONNECTED, const.IFACE_INACTIVE]: return True else: return False def connet_wifi(wifi_inter, wifi_profile): wifi_inter.remove_all_network_profiles() # 刪除其它配置文件 tmp_profile = wifi_inter.add_network_profile(wifi_profile) # 加載配置文件 wifi_inter.connect(tmp_profile) time.sleep(2) if wifi_inter.status() == const.IFACE_CONNECTED: return True else: return False def set_profile(): wifi_profile = pywifi.Profile() # 配置文件 wifi_profile.ssid = "我的wifi" # wifi名稱 wifi_profile.auth = const.AUTH_ALG_OPEN # 需要密碼 wifi_profile.akm.append(const.AKM_TYPE_WPA2PSK) # 加密類型 wifi_profile.cipher = const.CIPHER_TYPE_CCMP # 加密單元 wifi_profile.key = "1234567" # wifi密碼 return wifi_profile if __name__ == '__main__': wifi = pywifi.PyWiFi() interface = wifi.interfaces()[0] profile = set_profile() n = 0 while True: if not is_connected(interface): print('網(wǎng)絡(luò)已斷開,重新連接中……') con = connet_wifi(interface, profile) n += 1 if not con and n <= 3: continue else: res = '成功' if con else '失敗' print(f'嘗試連接{n}次,連接{res}!') n = 0 time.sleep(2)
python實(shí)現(xiàn)連接指定的wifi
# -*-coding:utf-8-*- import pywifi, time from pywifi import const # 1、python連接WiFi,需要使用pywifi包,安裝pywifi:pip install pywifi #注意:如果提示找不到comtypes,則還需要安裝pip install comtypes # 2、判斷wifi連接狀態(tài): def wifi_connect_status(): wifi = pywifi.PyWiFi() iface = wifi.interfaces()[0] # acquire the first Wlan card,maybe not if iface.status() in [const.IFACE_CONNECTED, const.IFACE_INACTIVE]: print("wifi connected!") return 1 else: print("wifi not connected!") return 0 # 3、掃描wifi: def scan_wifi(): wifi = pywifi.PyWiFi() iface = wifi.interfaces()[0] iface.scan() time.sleep(1) basewifi = iface.scan_results() for i in basewifi: print("wifi scan result:{}".format(i.ssid)) print("wifi device MAC address:{}".format(i.bssid)) return basewifi # 4、連接指定的wifi: def connect_wifi(): wifi = pywifi.PyWiFi() ifaces = wifi.interfaces()[0] print(ifaces.name()) # 輸出無線網(wǎng)卡名稱 ifaces.disconnect() time.sleep(3) profile = pywifi.Profile() # 配置文件 profile.ssid = "JYKC-31F" # wifi名稱 profile.auth = const.AUTH_ALG_OPEN # 需要密碼 profile.akm.append(const.AKM_TYPE_WPA2PSK) # 加密類型 profile.cipher = const.CIPHER_TYPE_CCMP # 加密單元 profile.key = "2151155511" # wifi密碼 ifaces.remove_all_network_profiles() # 刪除其它配置文件 tmp_profile = ifaces.add_network_profile(profile) # 加載配置文件 ifaces.connect(tmp_profile) time.sleep(5) isok = True if ifaces.status() == const.IFACE_CONNECTED: print("connect successfully!") else: print("connect failed!") time.sleep(1) return isok #5、測試 def main(): print("start") wifi_connect_status() scan_wifi() connect_wifi() print("finish!") if __name__ == "__main__": main()
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python orm 框架中sqlalchemy用法實(shí)例詳解
這篇文章主要介紹了python orm 框架中sqlalchemy用法,結(jié)合實(shí)例形式詳細(xì)分析了Python orm 框架基本概念、原理及sqlalchemy相關(guān)使用技巧,需要的朋友可以參考下2020-02-02Python的控制結(jié)構(gòu)之For、While、If循環(huán)問題
這篇文章主要介紹了Python的控制結(jié)構(gòu)之For、While、If循環(huán)問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06