Python 之 pywifi 使用文档

Pywifi 提供了一个用于操作无线接口的跨平台Python模块。

优点:易于使用 ; 支持 Windows 和Linux

1.Installation:

cd pywifi/
pip install .
Example:
import time
import pywifi
from pywifi import const

wifi = pywifi.PyWiFi()
iface = wifi.interfaces()[0]

iface.disconnect()

time.sleep(1)
assert iface.status() in\
    [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE]

profile = pywifi.Profile()
profile.ssid = 'testap'
profile.auth = const.AUTH_ALG_OPEN
profile.akm.append(const.AKM_TYPE_WPA2PSK)
profile.cipher = const.CIPHER_TYPE_CCMP
profile.key = '12345678'

iface.remove_all_network_profiles()
tmp_profile = iface.add_network_profile(profile)

iface.connect(tmp_profile)
time.sleep(30)
assert iface.status() == const.IFACE_CONNECTED

iface.disconnect()
time.sleep(1)
assert iface.status() in\
    [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE]

2.Docmentation:

Constants(常量):

pywifi 中定义了以下的 constatns。

Interface Status(接口状态):

Interface.status() 将返回以下 status code之一。

const.IFACE_DISCONNECTED
const.IFACE_SCANNING
const.IFACE_INACTIVE
const.IFACE_CONNECTING
const.IFACE_CONNECTED
Authentication Algorithms(认证算法):

认证算法应该与Profile文件相一致。在一般情况下,几乎所有的APs都使用开放算法。

const.AUTH_OPEN
const.AUTH_SHARED
Key Management Type(密钥管理类型)

密钥管理类型应该分配给Profile。
对于普通的APs,如果

AP没有安全设置,将Profile文件的akm设置为AKM_TYPE_NONE。
AP处于WPA模式,将Profile文件的akm设置为AKM_TYUPE_WPAPSK。
AP处于WPA2模式,将Profile文件的akm设置为AKM_TYUPE_WPA2PSK。

企业APs使用AKM_TYPE_WPA和AKM_TYPE_WPA2。

const.AKM_TYPE_NONE
const.AKM_TYPE_WPA
const.AKM_TYPE_WPAPSK
const.AKM_TYPE_WPA2
const.AKM_TYPE_WPA2PSK
Cipher Types(密码类型):

如果akm不是AKM_TYPE_NONE,则应该将密码类型设置为Profile。您可以参考要连接到的AP的设置。

const.CIPHER_TYPE_NONE
const.CIPHER_TYPE_WEP
const.CIPHER_TYPE_TKIP
const.CIPHER_TYPE_CCMP
Network Profile(网络配置文件):

Profile是我们要连接到的AP的设置。Profile的字段:

ssid - AP的ssid。
auth - AP的认证算法。
akm - AP的密钥管理类型。
cipher - AP的密码类型。
key (optinoal) -AP的key(需要输入的密码)。 如果密码不是CIPHER_TYPE_NONE,则应设置此值。
Example:
import pywifi

profile = pywifi.Profile()
profile.ssid = 'testap'
profile.auth = const.AUTH_ALG_OPEN
profile.akm.append(const.AKM_TYPE_WPA2PSK)
profile.cipher = const.CIPHER_TYPE_CCMP
profile.key = '12345678'

wifi = pywifi.PyWiFi()
iface = wifi.interfaces()[0]
profile = iface.add_network_profile(profile)
iface.connect(profile)

Interface(接口)

Interface是指我们用来执行Wi-Fi操作(如scan(扫描)、connect(连接)、disconnect(断开连接)等)的Wi-Fi interface。

Get interface information(获取接口信息):

一般来说,该平台只有一个Wi-Fi interface。因此,使用index 0获取Wi-Fi interface。

import pywifi

wifi = pywifi.PyWiFi()
iface = wifi.interfaces()[0]
Interface.name()   #获取wifi接口的名字
Interface.scan() #触发接口扫描APs
Interface.scan_results() #获取上次触发扫描的结果。将返回一个Profile文件列表。请注意。因为每个Wi-Fi接口的扫描时间是不同的。调用scan()后2~8秒调用scan_results()更安全。
Interface.add_network_profile(profile) #添加用于稍后连接的AP配置文件
Interface.remove_all_network_profiles() #删除所有AP配置
Interface.network_profiles() #通过返回Profile文件列表获取所有保存的AP Profile
Interface.connect(profile) #通过给定的profile连接到指定的AP。请注意,按照当前的设计,应该在调用connect(profile)之前调用add_network_profile(profile)
Interface.disconnect() #断开当前的AP连接
Interface.status() #获取当前status对象的status

猜你喜欢

转载自blog.csdn.net/QQB67G8COM/article/details/94360855