欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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用法實(shí)例詳解

    這篇文章主要介紹了python orm 框架中sqlalchemy用法,結(jié)合實(shí)例形式詳細(xì)分析了Python orm 框架基本概念、原理及sqlalchemy相關(guān)使用技巧,需要的朋友可以參考下
    2020-02-02
  • 每日python小白之如何打印九九乘法表

    每日python小白之如何打印九九乘法表

    這篇文章主要給大家介紹了關(guān)于用python如何打印九九乘法表的相關(guān)資料,包括初級版和完整版,初級版通過雙層循環(huán)輸出每個(gè)乘法表的行,完整版通過調(diào)整內(nèi)層循環(huán)的范圍,使得每行的乘法表輸出更加規(guī)范,文章還提供了代碼解釋和一些小技巧,需要的朋友可以參考下
    2024-11-11
  • Python的控制結(jié)構(gòu)之For、While、If循環(huán)問題

    Python的控制結(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
  • Python?if?else語句對縮進(jìn)的要求

    Python?if?else語句對縮進(jìn)的要求

    這篇文章主要介紹了Python?if?else語句對縮進(jìn)的要求,前面的一篇文章展示了選擇結(jié)構(gòu)的三種基本形式,并給出了實(shí)例演示,這篇文章基于上一篇內(nèi)容繼續(xù)對Python?if?else語句對縮進(jìn)進(jìn)行描述,需要的小伙伴可以參考一下
    2022-03-03
  • Jmeter中JSR223設(shè)置變量方式

    Jmeter中JSR223設(shè)置變量方式

    本文主要介紹了JMeter的幾種常用變量設(shè)置方式,特別對JSR223設(shè)置變量進(jìn)行了詳細(xì)解釋,JSR223是Java規(guī)范請求,可以向Java平臺增添新的API和服務(wù),JSR223Sampler可以使用JSR223腳本代碼執(zhí)行創(chuàng)建/更新變量所需的示例或一些計(jì)算
    2024-10-10
  • Python中pymysql 模塊的使用詳解

    Python中pymysql 模塊的使用詳解

    pymsql是Python中操作MySQL的模塊,其使用方法和MySQLdb幾乎相同。但目前pymysql支持python3.x而后者不支持3.x版本。
    2019-08-08
  • python將控制臺輸出保存至文件的方法

    python將控制臺輸出保存至文件的方法

    今天小編就為大家分享一篇python將控制臺輸出保存至文件的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python基于回溯法子集樹模板解決馬踏棋盤問題示例

    Python基于回溯法子集樹模板解決馬踏棋盤問題示例

    這篇文章主要介紹了Python基于回溯法子集樹模板解決馬踏棋盤問題,簡單描述了國際象棋馬踏棋盤問題,并結(jié)合實(shí)例形式分析了Python使用回溯法子集樹模板解決馬踏棋盤問題的具體步驟與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2017-09-09
  • Python如何讀取csv文件時(shí)添加表頭/列名

    Python如何讀取csv文件時(shí)添加表頭/列名

    這篇文章主要介紹了Python如何讀取csv文件時(shí)添加表頭/列名,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 使用matplotlib畫圖自定義marker

    使用matplotlib畫圖自定義marker

    這篇文章主要介紹了使用matplotlib畫圖自定義marker問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06

最新評論