pywifi模块

pywifi模块

pywifi的介绍与下载

1.介绍:pywifi是在python中一个用于操作无线接口的模块,可以跨平台使用,Windows和Linux都支持

2.下载:①pip下载:打开命令提示符,输入下载命令:

    pip install pywifi
    由于此模块基于comtypes模块,因此同时需要下载此模块:

    pip install comtypes
    对于PyCharm,则直接下载两个模块即可

pywifi基础

#  引入pywifi库及所带常量库
import pywifi
from pywifi import const, Profile
 
#  1. 基础
 
#  获取网卡接口
wifi = pywifi.PyWiFi()
 
#  得到第一个无线网卡
ifaces = wifi.interfaces()[0]
 
#  切断网卡连接
ifaces.disconnect()
 
#  获取wifi的连接状态
wifistatus = ifaces.status()
 
#  检查wifi是否处于切断状态
if wifistatus == const.IFACE_DISCONNECTED:
 
    #  网卡已被切断
 
    pass
 
#  如果网卡没有被切断
#  或者使用 " if wifistatus == const.IFACE_CONNECTED: "
 
else:
 
    #  已连接wifi
 
    pass
 
#  如果已经切断网卡,一般执行下述操作
if wifistatus == const.IFACE_DISCONNECTED:
 
    #  设置wifi连接文件
    profile: Profile = pywifi.Profile()
 
    #  你要连接的网络的名称
    profile.ssid = "    "
 
    #  网卡的开放状态
    #  " Auth - AP "的验证算法
    profile.auth = const.AUTH_ALG_OPEN
 
    #  wifi的加密算法
    #  通常的加密算法值为 " WPA "
    #  选择wifi的加密方式
    #  " Akm - AP "的密钥管理
    profile.akm.append(const.AKM_TYPE_WPA2PSK)
 
    #  加密单元
    #  " Cipher - AP "的密码类型
    profile.cipher = const.CIPHER_TYPE_CCMP
 
    #  设置密码
    password = "   "
 
    #  回调密码(wifi密码)
    #  如果没有密码,则设置值为 " CIPHER_TYPE_NONE "
    profile.key = password
 
    #  删除已连接的所有wifi文件
    ifaces.remove_all_network_profiles()
 
    #  加载新的wifi连接文件
    tep_profile = ifaces.add_network_profile(profile)
 
    #  连接上面的wifi文件
    ifaces.connect(tep_profile)
 
    #  如果wifi已连接
    if ifaces.status() == const.IFACE_CONNECTED:
 
        print(True)
 
    #  如果仍未连接
    else:
 
        print(False)

pywifi详细讲解

#  2.提高
 
#  获取wifi接口名称
name = ifaces.name()
 
#  扫描wifi ( AP )
ifaces.scan()
 
#  查看上面的wifi扫描结果 ( 返回值为列表 )
result = ifaces.scan_results()
 
#  删除所有的AP配置文件
#  目的是为了接下来的连接
ifaces.remove_all_network_profiles()
 
#  返回配置文件的列表
files = ifaces.network_profiles()
 
#  设置配置文件的名字
ifaces.add_network_profile(profile)
 
#  连接wifi
ifaces.connect(profile)
 
#  断开wifi
ifaces.disconnect()
 
#  wifi的连接状态
ifaces.status()
 
#  配置文件
profile = pywifi.Profile()
 
#  配置文件的方法
#  ssid  auth  akm  cipher  key
#  这些的详细讲解可看基础
 
 
#  pywifi中const的量
 
#  const.IFACE_DISCONNECTED = 0
#  const.IFACE_SCANNING = 1
#  const.IFACE_INACTIVE = 2
#  const.IFACE_CONNECTING = 3
#  const.IFACE_CONNECTED = 4
 
#  Auth - AP
var1 = const.AUTH_ALG_OPEN
var2 = const.AUTH_ALG_SHARED
 
#  Akm - AP
#  不安全的方法
var3 = const.AKM_TYPE_NONE
#  WPA的方法
var4 = const.AKM_TYPE_WPAPSK
#  WPA2的方法
var5 = const.AKM_TYPE_WPA2PSK
#  对于企业的方法
var6 = const.AKM_TYPE_WPA
var7 = const.AKM_TYPE_WPA2
 
#  Cipher - AP
var8 = const.CIPHER_TYPE_NONE  
var9 = const.CIPHER_TYPE_WEP
var10 = const.CIPHER_TYPE_TKIP
var11 = const.CIPHER_TYPE_CCMP
posted @ 2022-10-31 10:33  春游去动物园  阅读(1415)  评论(1编辑  收藏  举报