Pywifi用法 - python
生活随笔
收集整理的這篇文章主要介紹了
Pywifi用法 - python
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Pywifi - python用法
凉沐流風(fēng) - 楓
一、目錄??
一、目錄??
二、前言??
?三、pywifi的介紹與下載??
四、pywifi基礎(chǔ)??
五、pywifi詳細(xì)講解??
六、原本教程(英文版)??
二、前言??
本教程原本是我寄存在的一個(gè)python程序,所以所有的講解都是在代碼中的,若有不便,敬請(qǐng)諒解
原本我所書(shū)寫(xiě)的文件為英文版,教程可見(jiàn)下,若有疑問(wèn),可加Q:2633748531進(jìn)行詢問(wèn)
?
?三、pywifi的介紹與下載??
1.介紹:pywifi是在python中一個(gè)用于操作無(wú)線接口的模塊,可以跨平臺(tái)使用,Windows和Linux都支持
2.下載:①pip下載:打開(kāi)命令提示符,輸入下載命令:
pip install pywifi由于此模塊基于comtypes模塊,因此同時(shí)需要下載此模塊:
pip install comtypes對(duì)于PyCharm,則直接下載兩個(gè)模塊即可
四、pywifi基礎(chǔ)??
# 引入pywifi庫(kù)及所帶常量庫(kù) import pywifi from pywifi import const, Profile# 1. 基礎(chǔ)# 獲取網(wǎng)卡接口 wifi = pywifi.PyWiFi()# 得到第一個(gè)無(wú)線網(wǎng)卡 ifaces = wifi.interfaces()[0]# 切斷網(wǎng)卡連接 ifaces.disconnect()# 獲取wifi的連接狀態(tài) wifistatus = ifaces.status()# 檢查wifi是否處于切斷狀態(tài) if wifistatus == const.IFACE_DISCONNECTED:# 網(wǎng)卡已被切斷pass# 如果網(wǎng)卡沒(méi)有被切斷 # 或者使用 " if wifistatus == const.IFACE_CONNECTED: "else:# 已連接wifipass# 如果已經(jīng)切斷網(wǎng)卡,一般執(zhí)行下述操作 if wifistatus == const.IFACE_DISCONNECTED:# 設(shè)置wifi連接文件profile: Profile = pywifi.Profile()# 你要連接的網(wǎng)絡(luò)的名稱profile.ssid = " "# 網(wǎng)卡的開(kāi)放狀態(tài)# " Auth - AP "的驗(yàn)證算法profile.auth = const.AUTH_ALG_OPEN# wifi的加密算法# 通常的加密算法值為 " WPA "# 選擇wifi的加密方式# " Akm - AP "的密鑰管理profile.akm.append(const.AKM_TYPE_WPA2PSK)# 加密單元# " Cipher - AP "的密碼類型profile.cipher = const.CIPHER_TYPE_CCMP# 設(shè)置密碼password = " "# 回調(diào)密碼(wifi密碼)# 如果沒(méi)有密碼,則設(shè)置值為 " CIPHER_TYPE_NONE "profile.key = password# 刪除已連接的所有wifi文件ifaces.remove_all_network_profiles()# 加載新的wifi連接文件tep_profile = ifaces.add_network_profile(profile)# 連接上面的wifi文件ifaces.connect(tep_profile)# 如果wifi已連接if ifaces.status() == const.IFACE_CONNECTED:print(True)# 如果仍未連接else:print(False)五、pywifi詳細(xì)講解??
# 2.提高# 獲取wifi接口名稱 name = ifaces.name()# 掃描wifi ( AP ) ifaces.scan()# 查看上面的wifi掃描結(jié)果 ( 返回值為列表 ) result = ifaces.scan_results()# 刪除所有的AP配置文件 # 目的是為了接下來(lái)的連接 ifaces.remove_all_network_profiles()# 返回配置文件的列表 files = ifaces.network_profiles()# 設(shè)置配置文件的名字 ifaces.add_network_profile(profile)# 連接wifi ifaces.connect(profile)# 斷開(kāi)wifi ifaces.disconnect()# wifi的連接狀態(tài) ifaces.status()# 配置文件 profile = pywifi.Profile()# 配置文件的方法 # ssid auth akm cipher key # 這些的詳細(xì)講解可看基礎(chǔ)# pywifi中const的量# const.IFACE_DISCONNECTED = 0 # const.IFACE_SCANNING = 1 # const.IFACE_INACTIVE = 2 # const.IFACE_CONNECTING = 3 # const.IFACE_CONNECTED = 4# Auth - AP var1 = const.AUTH_ALG_OPEN var2 = const.AUTH_ALG_SHARED# Akm - AP # 不安全的方法 var3 = const.AKM_TYPE_NONE # WPA的方法 var4 = const.AKM_TYPE_WPAPSK # WPA2的方法 var5 = const.AKM_TYPE_WPA2PSK # 對(duì)于企業(yè)的方法 var6 = const.AKM_TYPE_WPA var7 = const.AKM_TYPE_WPA2# Cipher - AP var8 = const.CIPHER_TYPE_NONE var9 = const.CIPHER_TYPE_WEP var10 = const.CIPHER_TYPE_TKIP var11 = const.CIPHER_TYPE_CCMP六、原本教程(英文版)??
# Import the module of PyWifi import pywifi from pywifi import const, Profile# 1. Basic# Get the network card interface wifi = pywifi.PyWiFi()# Get the first wireless network card ifaces = wifi.interfaces()[0]# Disconnect the network card ifaces.disconnect()# Get the connection state of wifi wifistatus = ifaces.status()# Check if disconnect the wifi if wifistatus == const.IFACE_DISCONNECTED:# The network card disconnectpass# If the network card has already connected # Or use the " if wifistatus == const.IFACE_CONNECTED: "else:# Have already connected the wifipass# If you have already disconnect the network card, do the below if wifistatus == const.IFACE_DISCONNECTED:# Set up the file of wifi connectionprofile: Profile = pywifi.Profile()# The name of wifi which you want to connectprofile.ssid = "TP-LINKwangqing"# The opening state of the network card# The authentication algorithm of " Auth - AP"profile.auth = const.AUTH_ALG_OPEN# The encryption algorithm of wifi# The encryption algorithm of common wifi is " WPA "# Choose the encryption way of wifi# The key management type of " Akm - AP "profile.akm.append(const.AKM_TYPE_WPA2PSK)# Encryption unit# The password type of " Cipher - AP "profile.cipher = const.CIPHER_TYPE_CCMP# Set the passwordpassword = "wangzijia123456"# Call the password ( the password of wifi)# If there's no password, set the value " CIPHER_TYPE_NONE "profile.key = password# Delete all the file of wifi which has already connectedifaces.remove_all_network_profiles()# Loading the new file of connectiontep_profile = ifaces.add_network_profile(profile)# Connect the new file that aboveifaces.connect(tep_profile)# If the wifi has already connectedif ifaces.status() == const.IFACE_CONNECTED:print(True)# If it has disconnected yetelse:print(False)# 2.Improvement# Get the name of wifi interface name = ifaces.name()# Scan the wifi ( AP ) ifaces.scan()# Look the result of scanning ( list ) result = ifaces.scan_results()# Delete all the setting files of AP # In order to the next connect ifaces.remove_all_network_profiles()# Return the list of setting files files = ifaces.network_profiles()# Set the name of setting file ifaces.add_network_profile(profile)# Connect the wifi ifaces.connect(profile)# Disconnect the wifi ifaces.disconnect()# The connection state of wifi ifaces.status()# Const in pywifi# const.IFACE_DISCONNECTED = 0 # const.IFACE_SCANNING = 1 # const.IFACE_INACTIVE = 2 # const.IFACE_CONNECTING = 3 # const.IFACE_CONNECTED = 4# Set up the file profile = pywifi.Profile()# The way to set up the file # ssid auth akm cipher key # These look above# Auth - AP var1 = const.AUTH_ALG_OPEN var2 = const.AUTH_ALG_SHARED# Akm - AP # No safe mode var3 = const.AKM_TYPE_NONE # The mode of WPA var4 = const.AKM_TYPE_WPAPSK # The mode of WPA2 var5 = const.AKM_TYPE_WPA2PSK # For business var6 = const.AKM_TYPE_WPA var7 = const.AKM_TYPE_WPA2# Cipher - AP var8 = const.CIPHER_TYPE_NONE var9 = const.CIPHER_TYPE_WEP var10 = const.CIPHER_TYPE_TKIP var11 = const.CIPHER_TYPE_CCMP總結(jié)
以上是生活随笔為你收集整理的Pywifi用法 - python的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: QQ扫码登录实现与原理
- 下一篇: websocket python爬虫_p