1,python connect WiFi, Need to use pywifi package , install pywifi:pip install pywifi

2, judge wifi Connection status :
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, scanning 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, Connect the specified 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()) # Output wireless network card name ifaces.disconnect() time.sleep(3) profile =
pywifi.Profile() # configuration file profile.ssid = "123_wifi" #wifi name profile.auth =
const.AUTH_ALG_OPEN # Password required profile.akm.append(const.AKM_TYPE_WPA2PSK) # Encryption type
profile.cipher = const.CIPHER_TYPE_CCMP # Encryption unit profile.key = "88888888" #wifi password
ifaces.remove_all_network_profiles() # Delete other profiles tmp_profile =
ifaces.add_network_profile(profile) # Load 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

Complete test code :
#-*-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()) # Output wireless network card name ifaces.disconnect()
time.sleep(3) profile = pywifi.Profile() # configuration file profile.ssid = "123_wifi"
#wifi name profile.auth = const.AUTH_ALG_OPEN # Password required
profile.akm.append(const.AKM_TYPE_WPA2PSK) # Encryption type profile.cipher =
const.CIPHER_TYPE_CCMP # Encryption unit profile.key = "88888888" #wifi password
ifaces.remove_all_network_profiles() # Delete other profiles tmp_profile =
ifaces.add_network_profile(profile) # Load 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()

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage MySQL Basics Commonly used sentence ( Add / delete / modify query )C Language of a Gobang game implementation QT5.9 Use of learning notes QSqlQuery Method of Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control java Polymorphic array of web Front end signature plug-in _signature_pad Plug in implements electronic signature function centos7 install RabbitMqspringboot use redis Experiment 4 Automated test tools - software test