1、python连接WiFi,需要使用pywifi包,安装pywifi:pip install pywifi

2、判断wifi连接状态:
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
2、扫描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
3、连接指定的wifi:
# Define interface status. IFACE_DISCONNECTED = 0 IFACE_SCANNING = 1
IFACE_INACTIVE = 2 IFACE_CONNECTING = 3 IFACE_CONNECTED = 4 # Define auth
algorithms. AUTH_ALG_OPEN = 0 AUTH_ALG_SHARED = 1 # Define auth key mgmt types.
AKM_TYPE_NONE = 0 AKM_TYPE_WPA = 1 AKM_TYPE_WPAPSK = 2 AKM_TYPE_WPA2 = 3
AKM_TYPE_WPA2PSK = 4 AKM_TYPE_UNKNOWN = 5 # Define ciphers. CIPHER_TYPE_NONE =
0 CIPHER_TYPE_WEP = 1 CIPHER_TYPE_TKIP = 2 CIPHER_TYPE_CCMP = 3
CIPHER_TYPE_UNKNOWN = 4 # KEY_TYPE_NETWORKKEY = 0 # KEY_TYPE_PASSPHRASE = 1 def
connect_wifi(): wifi = pywifi.PyWiFi() ifaces = wifi.interfaces()[0]
print(ifaces.name()) #输出无线网卡名称 ifaces.disconnect() time.sleep(3) profile =
pywifi.Profile() #配置文件 profile.ssid = "123_wifi" #wifi名称 profile.auth =
const.AUTH_ALG_OPEN #需要密码 profile.akm.append(const.AKM_TYPE_WPA2PSK) #加密类型
profile.cipher = const.CIPHER_TYPE_CCMP #加密单元 profile.key = "88888888" #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

完整的测试代码:
#-*-coding:utf-8-*- import pywifi,time from pywifi import const 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 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 def connect_wifi(): wifi = pywifi.PyWiFi() ifaces =
wifi.interfaces()[0] print(ifaces.name()) #输出无线网卡名称 ifaces.disconnect()
time.sleep(3) profile = pywifi.Profile() #配置文件 profile.ssid = "123_wifi"
#wifi名称 profile.auth = const.AUTH_ALG_OPEN #需要密码
profile.akm.append(const.AKM_TYPE_WPA2PSK) #加密类型 profile.cipher =
const.CIPHER_TYPE_CCMP #加密单元 profile.key = "88888888" #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 def main(): print("start") wifi_connect_status() scan_wifi()
connect_wifi() print("finish!") if __name__ == "__main__": main()

技术
©2019-2020 Toolsou All rights reserved,
C语言——qsort函数CSS实现溢出显示省略号网络层协议——ICMP协议C语言小游戏-俄罗斯方块Qt入门教程【基础控件篇】QCalendarWidget日历控件用python来控制wifi连接vue中input框只能输入数字Python内置函数C语言数据结构-顺序表删除重复V2.0.0abaqus质量缩放系数取值_ABAQUS的质量缩放