pywifi详解

  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
pywifi模块是一个用于操作无线网卡的Python库。下面是pywifi模块的使用方法的详解: 1. 判断是否有无线网卡: 使用PyWiFi创建一个无线对象,然后通过wifi.interfaces()方法获取所有的无线网卡对象。判断网卡是否连接或者活动状态,如果是则输出"已连接",否则输出"未连接"。\[1\] 2. 扫描附近的WiFi并输出: 使用PyWiFi创建一个无线对象,然后通过wifi.interfaces()方法获取一个无线网卡对象。调用ifaces.scan()方法进行扫描,然后通过ifaces.scan_results()方法获取扫描结果。遍历扫描结果,输出每个WiFi的名称。\[1\] 3. 尝试并连接WiFi: 使用PyWiFi创建一个无线对象,然后通过wifi.interfaces()方法获取一个无线网卡对象。调用ifaces.disconnect()方法断开网卡连接,然后等待3秒。创建一个配置文件对象,设置WiFi的名称、认证方式、加密类型等参数。调用ifaces.remove_all_network_profiles()方法删除其他配置文件,然后调用ifaces.add_network_profile(profile)方法加载配置文件。调用ifaces.connect(tmp_profile)方法连接WiFi,然后等待10秒。判断连接状态,如果连接成功则输出"成功连接",否则输出"失败"。最后调用ifaces.disconnect()方法断开连接。\[2\] 以上就是pywifi模块的使用方法的详解。 #### 引用[.reference_title] - *1* *2* [PypywifiPython库之pywifi的简介、安装、使用方法之详细攻略](https://blog.csdn.net/qq_41185868/article/details/80396915)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值