121 lines
3.8 KiB
Python
121 lines
3.8 KiB
Python
import os
|
||
import re
|
||
import time
|
||
|
||
import psutil
|
||
import pywifi
|
||
from pywifi import const
|
||
|
||
from LogRecord.log_recorder import GLOBAL_LOG
|
||
|
||
|
||
def get_wifi_name():
|
||
echo = os.popen("netsh wlan show interfaces | findstr /C:\"SSID\"").read()
|
||
wifi_name_re = re.search(r"(?<=:\s).*", echo)
|
||
if wifi_name_re:
|
||
return wifi_name_re.group()
|
||
else:
|
||
return None
|
||
|
||
|
||
class WIFI:
|
||
|
||
def __init__(self) -> None:
|
||
super().__init__()
|
||
self.iface = None
|
||
self.wifi_device_initial()
|
||
self.log = GLOBAL_LOG
|
||
|
||
# WiFi驱动初始化
|
||
def wifi_device_initial(self):
|
||
while True:
|
||
try:
|
||
wifi = pywifi.PyWiFi()
|
||
self.iface = wifi.interfaces()[0] # acquire the first Wlan card,maybe not
|
||
break
|
||
except Exception as e:
|
||
self.log.write(f"WiFi驱动初始化失败,原因是:{e.args}。")
|
||
time.sleep(1)
|
||
continue
|
||
|
||
def get_connected_wifi_name(self):
|
||
try:
|
||
if self.wifi_connect_status():
|
||
wifi_name = get_wifi_name()
|
||
return wifi_name
|
||
except Exception as e:
|
||
self.log.write(f"获取连接的WiFi出错,原因是:{e.args}。")
|
||
return None
|
||
|
||
def wifi_connect_status(self):
|
||
try:
|
||
if self.iface.status() in [const.IFACE_CONNECTED, const.IFACE_INACTIVE]:
|
||
return True
|
||
else:
|
||
return False
|
||
except Exception as e:
|
||
self.log.write(f"获取WiFi状态出错,原因是:{e.args}。")
|
||
self.wifi_device_initial()
|
||
return False
|
||
|
||
def disconnect(self):
|
||
try:
|
||
while self.wifi_connect_status():
|
||
self.iface.disconnect()
|
||
except Exception as e:
|
||
self.log.write(f"断开WiFi出错,原因是:{e.args}。")
|
||
self.wifi_device_initial()
|
||
return False
|
||
|
||
@staticmethod
|
||
def get_ip():
|
||
dic = psutil.net_if_addrs()
|
||
container = {}
|
||
for adapter in dic:
|
||
snic_list = dic[adapter]
|
||
ipv4 = '无 ipv4 地址'
|
||
for snic in snic_list:
|
||
if snic.family.name == 'AF_INET':
|
||
ipv4 = snic.address
|
||
container[str(adapter)] = str(ipv4)
|
||
return container
|
||
|
||
def scan_wifi(self):
|
||
try:
|
||
self.iface.scan()
|
||
time.sleep(5)
|
||
base_wifi = self.iface.scan_results()
|
||
wifi_list = [str(wifi.ssid) for wifi in base_wifi]
|
||
return wifi_list
|
||
except Exception as e:
|
||
self.log.write(f"扫描WiFi出错,原因是:{e.args}。")
|
||
self.wifi_device_initial()
|
||
return []
|
||
|
||
def connect_wifi(self, ssid, password):
|
||
try:
|
||
self.disconnect()
|
||
for i in range(10):
|
||
time.sleep(1)
|
||
if not self.wifi_connect_status():
|
||
break
|
||
profile = pywifi.Profile() # 配置文件
|
||
profile.ssid = ssid # wifi名称
|
||
profile.auth = const.AUTH_ALG_OPEN # 需要密码
|
||
profile.akm.append(const.AKM_TYPE_WPA2PSK) # 加密类型
|
||
profile.cipher = const.CIPHER_TYPE_CCMP # 加密单元
|
||
profile.key = password # wifi密码
|
||
|
||
# self.iface.remove_all_network_profiles() # 删除其它配置文件
|
||
tmp_profile = self.iface.add_network_profile(profile) # 加载配置文件
|
||
self.iface.connect(tmp_profile)
|
||
for i in range(20):
|
||
time.sleep(1)
|
||
if self.iface.status() == const.IFACE_CONNECTED:
|
||
return True
|
||
return False
|
||
except Exception as e:
|
||
self.log.write(f"链接WiFi出错,原因是:{e.args}。")
|
||
self.wifi_device_initial()
|
||
return False
|