python如何實現(xiàn)wifi自動連接,解決電腦wifi經(jīng)常斷開問題
更新時間:2023年06月03日 14:57:22 作者:Achen's
這篇文章主要介紹了python實現(xiàn)wifi自動連接,解決電腦wifi經(jīng)常斷開的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
python實現(xiàn)wifi自動連接,解決電腦wifi經(jīng)常斷開
安裝pywifi模塊
pywifi依賴comtypes,不安裝會報錯
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實現(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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python自動化實現(xiàn)Word到圖片的轉(zhuǎn)換指南
在日常工作中,我們經(jīng)常會遇到需要將Word文檔內(nèi)容以圖片形式展示的場景,本文將深入探討如何利用Spire.Doc for Python這一庫,將Word文檔輕松轉(zhuǎn)換為高質(zhì)量圖片,感興趣的小伙伴可以了解下2026-01-01
python selenium爬取斗魚所有直播房間信息過程詳解
這篇文章主要介紹了python selenium爬取斗魚所有直播房間信息過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08
Python startswith()和endswith() 方法原理解析
這篇文章主要介紹了Python startswith()和endswith() 方法原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04
Python中使用PyExecJS庫執(zhí)行JavaScript函數(shù)
Python在運行JavaScript函數(shù)時,需要用到外部庫來執(zhí)行JavaScript,本文主要介紹了Python中使用PyExecJS庫執(zhí)行JavaScript函數(shù),具有一定的參考價值,感興趣的可以了解一下2024-04-04
在VS2017中用C#調(diào)用python腳本的實現(xiàn)
這篇文章主要介紹了在VS2017中用C#調(diào)用python腳本的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
Python初學者必須掌握的25個內(nèi)置函數(shù)詳解
這篇文章主要介紹了Python25個常用內(nèi)置函數(shù)總結(jié),本文羅列了數(shù)學相關(guān) 、功能相關(guān)、類型轉(zhuǎn)換、字符串處理、序列處理函數(shù)等常用內(nèi)置函數(shù),需要的朋友可以參考下2021-09-09
django開發(fā)post接口簡單案例,獲取參數(shù)值的方法
今天小編就為大家分享一篇django開發(fā)post接口簡單案例,獲取參數(shù)值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12

