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. # 获取wifi接口的名字
Interface.name()
2. # 扫描APs
Interface.scan()
3. # 获取上次触发扫描的结果。将返回一个Profile文件列表
Interface.scan_results()
4. # 添加用于稍后连接的AP配置文件
Interface.add_network_profile(profile)
5. # 删除所有AP配置
Interface.remove_all_network_profiles()
6. # 通过返回Profile文件列表获取所有保存的AP Profile
tep_profile = Interface.network_profiles(profile)
7. # 调用add_network_profile(profile)之后,连接设置的AP Profile
Interface.connect(tep_profile)
8. # 断开当前的AP连接
Interface.disconnect()
9. # 获取当前wifi的status
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
联系客服