打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
pywifi详解
userphoto

2022.10.07 辽宁

关注
  1. 安装库
pip install pywifi
  1. 成员变量
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. 常用方法:
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)
  1. 常用函数封装
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
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Python攻防
python自动化:多线程枚举获取wifi信息(你懂的)!
WiFi密码怎么破?一个Python脚本搞定
hacker:Python通过对简单的WIFI弱口令实现自动实时破解
debian系统配置ip地址、重启网络、DNS配置
python可以操作wifi吗?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服