網(wǎng)站首頁 編程語言 正文
??跨平臺的pywifi模塊支持操作無線網(wǎng)卡,該模塊易于使用,同時支持Windows、Linux等多個系統(tǒng)。pywifi模塊不是Python的標準模塊,需單獨安裝,同時該模塊依賴comtypes模塊,最好同時安裝comtypes模塊,否則調用pywifi的函數(shù)時可能會報錯。
pip install comtypes
pip install pywifi
??pywifi模塊中的類不算太多,其中主要用到的類包括PyWiFi、Profile、Interface等,詳述如下:
??PyWiFi類用于操作無線設備,該類的主要函數(shù)interfaces返回可用的無線網(wǎng)卡集合,也即Interface對象集合。
??Profile類表示無線接入點(AP),也即無線網(wǎng)卡搜索出的無線連接,一個Profile對象表示一個可以連接或可用的無線連接,Profile類的主要屬性如下表所示:
序號 | 屬性名 | 說明 |
---|---|---|
1 | ssid | 無線網(wǎng)絡名稱 |
2 | auth | 認證算法,包括AUTH_ALG_OPEN、.AUTH_ALG_SHARED兩種,默認為AUTH_ALG_OPEN,關于認證算法的介紹詳見參考文獻5 |
3 | akm | 授權密鑰管理方式,包括AKM_TYPE_NONE、AKM_TYPE_WPA、AKM_TYPE_WPAPSK、AKM_TYPE_WPA2、AKM_TYPE_WPA2PSK、AKM_TYPE_UNKNOWN,默認為AKM_TYPE_NONE,關于授權密鑰管理方式詳見參考文獻6-7 |
4 | cipher | 密碼類型,包括CIPHER_TYPE_NONE、CIPHER_TYPE_WEP、CIPHER_TYPE_TKIP、CIPHER_TYPE_CCMP、CIPHER_TYPE_UNKNOWN,默認為CIPHER_TYPE_NONE |
5 | key | 無線網(wǎng)絡連接密碼,如果密碼類型不未CIPHER_TYPE_NONE,則應設置本屬性值 |
??;Interface類用于執(zhí)行無線網(wǎng)絡操作,主要包括以下函數(shù):
序號 | 函數(shù)名 | 說明 |
---|---|---|
1 | name | 獲取無線網(wǎng)卡名稱 |
2 | scan | 調用無線網(wǎng)卡掃描可用的無線網(wǎng)絡(AP) |
3 | scan_results | 獲取scan函數(shù)的掃描結果,返回的是Profile對象列表 |
4 | add_network_profile | 添加特定無線網(wǎng)絡(AP)以便后續(xù)連接 |
5 | remove_network_profile | 移除指定的無線網(wǎng)絡(AP) |
6 | remove_all_network_profiles | 移除所有無線網(wǎng)絡(AP) |
7 | network_profiles | 獲取保存的所有無線網(wǎng)絡(AP) |
8 | connect | 連接指定的無線網(wǎng)絡(AP) |
9 | disconnect | 斷掉當前無線網(wǎng)絡連接 |
10 | status | 獲取當前無線網(wǎng)絡連接狀態(tài) |
??最后是照著參考文獻3編寫的測試程序(測試前請確保已記住當前計算機連接的wifi密碼)
import pywifi
wifi = pywifi.PyWiFi()
iface = wifi.interfaces()[0]
print('interface name: ',iface.name())
print('interface status: ',iface.status())
iface.disconnect()
print('interface status: ',iface.status())
profile = pywifi.Profile()
profile.ssid = "XXXXXXX"
profile.auth = pywifi.const.AUTH_ALG_OPEN
profile.akm.append(pywifi.const.AKM_TYPE_WPA2PSK)
profile.cipher = pywifi.const.CIPHER_TYPE_CCMP
profile.key = 'XXXXXXX'
iface.remove_all_network_profiles()
tep_profile = iface.add_network_profile(profile)
iface.connect(tep_profile)
sleep(5)
print('interface status: ',iface.status())
參考文獻
[1]https://github.com/awkman/pywifi
[2]https://github.com/awkman/pywifi/blob/master/DOC.md
[3]https://blog.csdn.net/Feng_liangmu/article/details/122911002
[4]https://docs.python.org/zh-cn/3/index.html
[5]https://www.likecs.com/show-655827.html
[6]https://www.likecs.com/show-655827.html
[7]https://code84.com/739564.html
原文鏈接:https://blog.csdn.net/gc_2299/article/details/128058252
相關推薦
- 2022-08-13 Linux內核中container_of宏定義講解
- 2022-03-31 C#實現(xiàn)學生檔案查詢_C#教程
- 2022-11-12 C語言用遞歸函數(shù)對素數(shù)進行判斷流程_C 語言
- 2022-04-24 C/C++字節(jié)序的深入理解_C 語言
- 2022-05-15 Web?API身份認證解決方案之Basic基礎認證_實用技巧
- 2022-09-21 flutter實現(xiàn)底部不規(guī)則導航欄_Android
- 2022-10-02 利用Android封裝一個有趣的Loading組件_Android
- 2023-08-12 JQ 實現(xiàn)復制到剪切板
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支