- 安装库
pip install pywifi
- 成员变量
profile = pywifi.Profile()
profile.ssid = ssid
profile.auth = auth
profile.akm.append(akm)
profile.cipher = cipher
if cipher != const.CIPHER_TYPE_NONE:
profile.key = key
1. ssid:AP的用户名
2. auth:AP的认证算法
- const.AUTH_OPEN
- const.AUTH_SHARED
3. akm:AP的密钥管理类型
- const.AKM_TYPE_NONE
- const.AKM_TYPE_WPA
- const.AKM_TYPE_WPAPSK
- const.AKM_TYPE_WPA2
- const.AKM_TYPE_WPA2PSK
4. cipher:AP的密码类型
- const.CIPHER_TYPE_NONE
- const.CIPHER_TYPE_WEP
- const.CIPHER_TYPE_TKIP
- const.CIPHER_TYPE_CCMP
5. key:AP的密码
- 如果cipher不是CIPHER_TYPE_NONE,则应设置此值。
- 常用方法:
1.
Interface.name()
2.
Interface.scan()
3.
Interface.scan_results()
4.
Interface.add_network_profile(profile)
5.
Interface.remove_all_network_profiles()
6.
tep_profile = Interface.network_profiles(profile)
7.
Interface.connect(tep_profile)
8.
Interface.disconnect()
9.
Interface.status()
- const.IFACE_DISCONNECTED
- const.IFACE_SCANNING
- const.IFACE_INACTIVE
- const.IFACE_CONNECTING
- const.IFACE_CONNECTED)
- 常用函数封装
import time
import pywifi
from pywifi import const
def connect_wifi(ssid, auth, akm, cipher, key):
wifi = pywifi.PyWiFi()
iface = wifi.interfaces()[0]
iface.disconnect()
time.sleep(2)
wifi_status = iface.status()
if wifi_status == const.IFACE_DISCONNECTED:
profile = pywifi.Profile()
profile.ssid = ssid
profile.auth = auth
profile.akm.append(akm)
profile.cipher = cipher
if cipher != const.CIPHER_TYPE_NONE:
profile.key = key
iface.remove_all_network_profiles()
tep_profile = iface.add_network_profile(profile)
iface.connect(tep_profile)
time.sleep(2)
if iface.status() == const.IFACE_CONNECTED:
return True
return False