当前位置:
首页 > Python基础教程 >
-
Pywifi用法-python
一、目录
1.目录
2.pywifi的介绍与下载
3.pywifi基础
4.pywifi详细教程
5.后言
6.原本教程(英文版)
二、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
五、后言
本教程原本是我寄存在的一个python程序,所以所有的讲解都是在代码中的,若有不便,敬请谅解
原本我所书写的文件为英文版,教程可见下,若有疑问,可加Q:2633748531进行询问
六、原本教程(英文版)
# Import the module of PyWifi
import pywifi
from pywifi import const, Profile
# 1. Basic
# Get the network card interface
wifi = pywifi.PyWiFi()
# Get the first wireless network card
ifaces = wifi.interfaces()[0]
# Disconnect the network card
ifaces.disconnect()
# Get the connection state of wifi
wifistatus = ifaces.status()
# Check if disconnect the wifi
if wifistatus == const.IFACE_DISCONNECTED:
# The network card disconnect
pass
# If the network card has already connected
# Or use the " if wifistatus == const.IFACE_CONNECTED: "
else:
# Have already connected the wifi
pass
# If you have already disconnect the network card, do the below
if wifistatus == const.IFACE_DISCONNECTED:
# Set up the file of wifi connection
profile: Profile = pywifi.Profile()
# The name of wifi which you want to connect
profile.ssid = "TP-LINKwangqing"
# The opening state of the network card
# The authentication algorithm of " Auth - AP"
profile.auth = const.AUTH_ALG_OPEN
# The encryption algorithm of wifi
# The encryption algorithm of common wifi is " WPA "
# Choose the encryption way of wifi
# The key management type of " Akm - AP "
profile.akm.append(const.AKM_TYPE_WPA2PSK)
# Encryption unit
# The password type of " Cipher - AP "
profile.cipher = const.CIPHER_TYPE_CCMP
# Set the password
password = "wangzijia123456"
# Call the password ( the password of wifi)
# If there's no password, set the value " CIPHER_TYPE_NONE "
profile.key = password
# Delete all the file of wifi which has already connected
ifaces.remove_all_network_profiles()
# Loading the new file of connection
tep_profile = ifaces.add_network_profile(profile)
# Connect the new file that above
ifaces.connect(tep_profile)
# If the wifi has already connected
if ifaces.status() == const.IFACE_CONNECTED:
print(True)
# If it has disconnected yet
else:
print(False)
# 2.Improvement
# Get the name of wifi interface
name = ifaces.name()
# Scan the wifi ( AP )
ifaces.scan()
# Look the result of scanning ( list )
result = ifaces.scan_results()
# Delete all the setting files of AP
# In order to the next connect
ifaces.remove_all_network_profiles()
# Return the list of setting files
files = ifaces.network_profiles()
# Set the name of setting file
ifaces.add_network_profile(profile)
# Connect the wifi
ifaces.connect(profile)
# Disconnect the wifi
ifaces.disconnect()
# The connection state of wifi
ifaces.status()
# Const in pywifi
# const.IFACE_DISCONNECTED = 0
# const.IFACE_SCANNING = 1
# const.IFACE_INACTIVE = 2
# const.IFACE_CONNECTING = 3
# const.IFACE_CONNECTED = 4
# Set up the file
profile = pywifi.Profile()
# The way to set up the file
# ssid auth akm cipher key
# These look above
# Auth - AP
var1 = const.AUTH_ALG_OPEN
var2 = const.AUTH_ALG_SHARED
# Akm - AP
# No safe mode
var3 = const.AKM_TYPE_NONE
# The mode of WPA
var4 = const.AKM_TYPE_WPAPSK
# The mode of WPA2
var5 = const.AKM_TYPE_WPA2PSK
# For business
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
出处:https://www.cnblogs.com/program-feng/p/15889644.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式