網站首頁 編程語言 正文
??跨平臺的pywifi模塊支持操作無線網卡,該模塊易于使用,同時支持Windows、Linux等多個系統。pywifi模塊不是Python的標準模塊,需單獨安裝,同時該模塊依賴comtypes模塊,最好同時安裝comtypes模塊,否則調用pywifi的函數時可能會報錯。
pip install comtypes
pip install pywifi
??pywifi模塊中的類不算太多,其中主要用到的類包括PyWiFi、Profile、Interface等,詳述如下:
??PyWiFi類用于操作無線設備,該類的主要函數interfaces返回可用的無線網卡集合,也即Interface對象集合。
??Profile類表示無線接入點(AP),也即無線網卡搜索出的無線連接,一個Profile對象表示一個可以連接或可用的無線連接,Profile類的主要屬性如下表所示:
序號 | 屬性名 | 說明 |
---|---|---|
1 | ssid | 無線網絡名稱 |
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 | 無線網絡連接密碼,如果密碼類型不未CIPHER_TYPE_NONE,則應設置本屬性值 |
??;Interface類用于執行無線網絡操作,主要包括以下函數:
序號 | 函數名 | 說明 |
---|---|---|
1 | name | 獲取無線網卡名稱 |
2 | scan | 調用無線網卡掃描可用的無線網絡(AP) |
3 | scan_results | 獲取scan函數的掃描結果,返回的是Profile對象列表 |
4 | add_network_profile | 添加特定無線網絡(AP)以便后續連接 |
5 | remove_network_profile | 移除指定的無線網絡(AP) |
6 | remove_all_network_profiles | 移除所有無線網絡(AP) |
7 | network_profiles | 獲取保存的所有無線網絡(AP) |
8 | connect | 連接指定的無線網絡(AP) |
9 | disconnect | 斷掉當前無線網絡連接 |
10 | status | 獲取當前無線網絡連接狀態 |
??最后是照著參考文獻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-12-11 Redis?Cluster原理及配置詳解_Redis
- 2022-08-07 Python算法練習之二分查找算法的實現_python
- 2022-12-25 Redis對象與redisObject超詳細分析源碼層_Redis
- 2022-04-08 詳解RIFF和WAVE音頻文件格式_相關技巧
- 2023-02-23 Python創建7種不同的文件格式的方法總結_python
- 2022-04-12 Apache?Pulsar集群搭建部署詳細過程_Linux
- 2022-04-10 WPF關鍵幀動畫介紹與實現_實用技巧
- 2022-09-17 Pandas查詢數據df.query的使用_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支