-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwifiConnect.py
96 lines (73 loc) · 2.83 KB
/
wifiConnect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from pywifi import PyWiFi, Profile
from time import sleep
from ruijie import shuConnect
from psutil import net_if_stats
from netName import netHiWifi, netHiWire
from wireConnect import wire_connect_status
from PyQt5.QtCore import QThread, pyqtSignal
nameHiwire = netHiWire()
nameHiwifi = netHiWifi()
class wifiSHU():
s1 = '无线连接前,请先断开网线\n'
s2 = "无线网卡未打开,请打开 WLAN 开关\n"
s3 = '未检测到无线网卡,或其他未知原因\n'
def __init__(self, user, passwd):
self.user = user
self.passwd = passwd
def wifi_connect_status(self):
checkTag = wire_connect_status()
if checkTag is None:
try:
if not net_if_stats()[nameHiwifi].isup: # wifi未连接到 internet 或未打开WiFi开关
# s = "无线网卡未打开,请打开 WLAN 开关\n"
return 2
else: # wifi已连接到 Shu(ForAll)
return 4
except:
# s = '未检测到无线网卡,或其他未知原因: ' + str(e)
return 3
else:
# s = '无线连接前,请先断开网线\n'
return 1
def wifi_connect(self):
wifiTag = self.wifi_connect_status()
if wifiTag == 1:
return self.s1
elif wifiTag == 2 or 4:
try:
shu = shuConnect(self.user, self.passwd)
msg = shu.start_connect()
if '暂时无法认证' in msg:
return
else: # wifi已连接到 Shu(ForAll)
s0 = "Shu(ForAll) 连接成功\n"
s = s0 + msg
return s
except:
return self.s3
elif wifiTag == 3:
return self.s3
class wifiCon(QThread):
signalCon = pyqtSignal(str)
def __init__(self, user, passwd, sender):
super().__init__()
self.user = user
self.passwd = passwd
self.signalCon.connect(sender.callback)
def run(self):
try:
wifi = PyWiFi() # 创建一个wifi对象
iface = wifi.interfaces()[0] # 取第一个无限网卡
iface.disconnect() # 断开网卡连接
profile = Profile() # 配置文件
profile.ssid = "Shu(ForAll)" # wifi名称
iface.remove_all_network_profiles() # 删除其他配置文件
tmp_profile = iface.add_network_profile(profile) # 加载配置文件
iface.connect(tmp_profile) # 连接
sleep(1) # 不延时,wifi反应不过来
s0 = "Shu(ForAll) 连接成功\n"
shu = shuConnect(self.user, self.passwd, chose=2)
s = s0 + shu.start_connect()
except:
s = 'macError'
self.signalCon.emit(s)