ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python脚本切换wifi-pywifi使用

2022-08-07 10:01:10  阅读:326  来源: 互联网

标签:profile const ifaces python wifi pywifi TYPE


1.前言

 

2.安装

 安装python包pywifi,由于依赖comtypes,使用的时候还得装上comtypes。

pip install pywifi
pip install comtypes

 

3.教程文档

#  引入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)
 #  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
 
View Code
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,则应设置此值。

  

 

4.基本用法

连接WIFI

def connect_wifi():
    wifi = pywifi.PyWiFi()  # 创建一个wifi对象
    ifaces = wifi.interfaces()[0]  # 取第一个无限网卡
    print(ifaces.name())  # 输出无线网卡名称
    ifaces.disconnect()  # 断开网卡连接
    time.sleep(3)  # 缓冲3秒

    profile = pywifi.Profile()  # 配置文件
    profile.ssid = ""  # wifi名称
    profile.auth = const.AUTH_ALG_OPEN  # 需要密码
    profile.akm.append(const.AKM_TYPE_WPA2PSK)  # 加密类型
    profile.cipher = const.CIPHER_TYPE_CCMP  # 加密单元
    profile.key = ''  # wifi密码

    ifaces.remove_all_network_profiles()  # 删除其他配置文件
    tmp_profile = ifaces.add_network_profile(profile)  # 加载配置文件

    ifaces.connect(tmp_profile)  # 连接
    time.sleep(5)  # 5秒后检查连接状态
    isok = True
    if ifaces.status() == const.IFACE_CONNECTED:
        print("成功连接")
    else:
        print("失败")
    # ifaces.disconnect()  # 断开连接
    time.sleep(1)
    return isok

 扫描WIFI

def scan_wifi():
    # 扫描附近wifi
    wifi = pywifi.PyWiFi()
    iface = wifi.interfaces()[0]

    iface.scan()
    time.sleep(1)
    basewifi = iface.scan_results()
    # for i in basewifi:
    #     print('wifi扫描结果:{}'.format(i.ssid))  # ssid 为wifi名称
    #     print('wifi设备MAC地址:{}'.format(i.bssid))
    return basewifi

  

 

标签:profile,const,ifaces,python,wifi,pywifi,TYPE
来源: https://www.cnblogs.com/Heyyy/p/16556649.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有