forked from nicedayzhu/netAssist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
296 lines (273 loc) · 10.6 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/1/4 20:25
# @Author : SeniorZhu1994
# @Site :
# @File : main.py.py
# @Software: PyCharm
# -*- coding: utf-8 -*-
import sys
import tcp_Logic
import udp_Logic
import tcp_udp_ui
import PyQt5.sip
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy, \
QLabel, QPushButton, QFileDialog, QMessageBox
from netAssitui import Ui_NetAssist
# from QCandyUi import CandyWindow
# import qdarkstyle
from PyQt5.QtCore import QTimer
import os
class PyQt5_Netassist(QMainWindow, tcp_Logic.TcpLogic,
udp_Logic.UdpLogic, tcp_udp_ui.Tcp_ucpUi, Ui_NetAssist):
def __init__(self):
# Python3中的继承只用一个super()就可以了,继承后初始化父类的属性
super(PyQt5_Netassist, self).__init__()
self.setupUi(self)
self.working = False
self.newline.setChecked(1)
self.remoteip_lbl.hide()
self.remoteip_text.hide()
self.remoteport_text.hide()
self.remoteport_lbl.hide()
self.init()
self.custom_connect()
self.init_statusbar()
self.open_btn.setToolTip("打开连接")
def init(self):
# 打印选择的协议类型编号
self.prot_box.currentTextChanged.connect(self.proto_imf)
# 对open_btn按下进行判断:TCPserver or TCPClient
self.open_btn.clicked.connect(self.click_select_open)
# 关闭socket
self.close_btn.clicked.connect(self.click_select_close)
# 按钮发送数据
self.send_Btn.clicked.connect(self.data_send_select)
# 文件发送按钮
self.file_send_btn.clicked.connect(self.file_send_select)
# 清空接收区显示
self.clr_btn.clicked.connect(self.recv_dataclear)
# 清空发送区显示
self.clr_btn2.clicked.connect(self.send_dataclear)
# 当标记状态改变时触发信号,recv2file的isChecked状态作为状态改变的参考
self.recv2file.toggled.connect(self.rfilechoose)
# 按下保存数据按钮,进行保存操作
self.save_btn.clicked.connect(self.datasave2file)
# 载入需要发送的文件
self.file_load.toggled.connect(self.send_fileload)
# 定时器启动检测
self.Sendloop.toggled.connect(self.checktimer)
# 发送校验位选择
self.Sendcheck.toggled.connect(self.checksend_choose)
def init_statusbar(self):
# 设置statusbar所有控件自动延伸
self.statusbar.setSizePolicy(QSizePolicy.Expanding,
QSizePolicy.Expanding)
# 设置status隐藏控制点(靠齐最右边)
self.statusbar.setSizeGripEnabled(False)
self.statusbar_dict['status'] = QLabel()
self.statusbar_dict['status'].setText('状态:Ready')
self.statusbar_dict['tx'] = QLabel()
# self.statusbar_dict['space']=QSpacerItem(20,20,QSizePolicy.Expanding,QSizePolicy.Expanding)
self.statusbar_dict['tx'].setText('发送计数:0')
self.statusbar_dict['rx'] = QLabel()
self.statusbar_dict['rx'].setText('接收计数:0')
self.statusbar_dict['clear'] = QPushButton()
self.statusbar_dict['clear'].setText('清除')
self.statusbar_dict['clear'].setToolTip('清除发送和接收计数')
self.statusbar_dict['clear'].pressed.connect(
self.statusbar_clear_pressed)
for i, w in enumerate(self.statusbar_dict.values()):
if i != len(self.statusbar_dict) - 1:
self.statusbar.addWidget(w, 20)
else:
# 最后一个控件不拉伸
self.statusbar.addWidget(w)
def statusbar_clear_pressed(self):
self.statusbar_dict['tx'].setText('发送计数:0')
self.statusbar_dict['rx'].setText('接收计数:0')
self.rx_count = 0
self.tx_count = 0
def checktimer(self):
'''
检测Timer是否需要开启
:return:
'''
if self.Sendloop.isChecked():
self.timer = QTimer(self)
try:
self.interval = int(self.loopinterval.text())
if self.file_load.isChecked():
# 下面connect横线是pycharm的一个bug,不需理会
self.timer.timeout.connect(self.file_send_select)
else:
# 下面connect横线是pycharm的一个bug,不需理会
self.timer.timeout.connect(self.data_send_select)
self.timer.start(self.interval)
except Exception as ret:
self.messagebox_info('请输入合法的时间间隔/ms')
self.Sendloop.setChecked(0)
else:
self.timer.stop()
def click_select_open(self):
'''
启动按钮功能选择
:return:
'''
self.prot_box.setEnabled(0)
if self.prot_box.currentIndex() == 0:
# 创建TCPServer
self.socket_open_tcps()
self.clients_list.addItem('All Connections')
if self.prot_box.currentIndex() == 1:
# 创建TCPClient
self.socket_open_tcpc()
if self.prot_box.currentIndex() == 2:
# 创建UDPClient socket
self.socket_open_udp()
if self.working is True:
self.statusbar_dict['status'].setText('状态:打开')
def click_select_close(self):
'''
断开按钮功能选择
:return:
'''
if self.Sendloop.isChecked():
self.timer.stop()
if self.prot_box.currentIndex() == 0:
# 关闭TCPServer
self.socket_close()
if self.prot_box.currentIndex() == 1:
# 断开TCPClient
self.socket_close()
if self.prot_box.currentIndex() == 2:
# 关闭UDP socket
self.socket_close_u()
self.prot_box.setEnabled(1)
self.statusbar_dict['status'].setText('状态:关闭')
def data_send_select(self):
''' 发送按钮功能选择
:return:
'''
if self.prot_box.currentIndex() == 0:
self.data_send_t()
if self.prot_box.currentIndex() == 1:
self.data_send_t_c()
if self.prot_box.currentIndex() == 2:
self.data_send_u()
def file_send_select(self):
'''
文件发送功能
:return:
'''
if self.prot_box.currentIndex() == 0:
self.file_send_t()
if self.prot_box.currentIndex() == 1:
self.file_send_t_c()
if self.prot_box.currentIndex() == 2:
self.file_send_u()
def proto_imf(self):
# 协议类型选择
imf_s = self.prot_box.currentIndex()
if imf_s == 0:
self.clients_list.clear()
self.localip_lb.setText('2.本地ip地址')
self.localport_lb.setText('3.本地端口号')
self.open_btn.setText('开始监听')
self.clients_lbl.setText('客户端:')
# 在tcp下不显示udp下的远程ip,远程端口标签
self.clients_list.show()
self.clients_lbl.show()
self.remoteip_lbl.hide()
self.remoteip_text.hide()
self.remoteport_text.hide()
self.remoteport_lbl.hide()
if imf_s == 1:
self.localip_lb.setText('2.远程ip地址')
self.localport_lb.setText('3.远程端口号')
self.open_btn.setText('开始连接')
self.clients_lbl.setText('远程主机:')
self.clients_list.clear()
if imf_s == 2:
self.localip_lb.setText('2.本地ip地址')
self.localport_lb.setText('3.本地端口号')
self.open_btn.setText('开始监听')
self.clients_lbl.setText('客户端:')
self.remoteip_lbl.show()
self.remoteip_text.show()
self.remoteport_text.show()
self.remoteport_lbl.show()
self.clients_list.hide()
self.clients_lbl.hide()
def recv_dataclear(self):
"""
pushbutton_clear控件点击触发的槽
:return: None
"""
# 清空接收区屏幕
self.DataRecvtext.clear()
def send_dataclear(self):
# 清空发送区框内容
self.DataSendtext.clear()
def rfilechoose(self):
'''
选择要保存的文件名
:return:
'''
if self.recv2file.isChecked():
'''接收转向文件'''
file_name, ok = QFileDialog.getSaveFileName(
self, u'保存文件', './', u'所有文件(*.*)')
print(file_name)
if ok:
self.save_file_name = file_name
else:
self.save_file_name = None
def datasave2file(self):
'''
将接收框中的消息保存到文件
:return:
'''
if not self.DataRecvtext.toPlainText():
QMessageBox.critical(self, '警告', '当前没有需要的数据')
else:
file_name, state = QFileDialog.getSaveFileName(self, '保存文件', './',
'Text文件(*.txt)')
if state:
with open(file_name, 'a', encoding='utf-8') as f_obj:
f_obj.write(self.DataRecvtext.toPlainText())
QMessageBox.information(self, '成功', '%s文件保存成功! ' % file_name)
def closeEvent(self, event):
"""
对MainWindow的函数closeEvent进行重构
退出软件时结束所有进程
:param event:
:return:
"""
reply = QtWidgets.QMessageBox.question(self,
'NetAssist',
"是否要退出程序?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes:
event.accept()
os._exit(0)
else:
event.ignore()
if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = PyQt5_Netassist()
# 添加界面美化QCandyUI
# myWin = CandyWindow.createWindow(myWin, 'blueGreen')
# 如下是把翻译文件切换为中文
translator = QtCore.QTranslator()
translator.load("widgets_zh_CN.qm")
app.installTranslator(translator)
# app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
qss = """
QPushButton#open_btn:pressed {background-color: blue}
"""
myWin.setStyleSheet(qss)
myWin.show()
sys.exit(app.exec_())