easyPing工具
目录结构
easyPing.py
# -*- coding: utf-8 -*- import asyncio import re import sys import aioping from PyQt5 import QtCore, QtWidgets from MyPing import Ui_MyPing class EasyPing(QtWidgets.QWidget): ''' 检测某网段的IP使用情况 ''' _ping_signal = QtCore.pyqtSignal(bool, str) def __init__(self, parent=None): super(EasyPing, self).__init__() self.ui = Ui_MyPing() self.ui.setupUi(self) self.ui.startIP.editingFinished.connect(self.set_end_ip) self.ui.pingButton.clicked.connect(self.run) self._ping_signal.connect(self.set_ui) def set_end_ip(self): ''' 填写起始地址后,默认填写结束地址为xxx.xxx.xxx.255 ''' startip = self.ui.startIP.text() pattern = r"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))$)" m = re.match(pattern, startip) # 检查IP地址是否合法 if m: startip = startip.split('.') startip[3] = '255' endip = '.'.join(startip) self.ui.endIP.setText(endip) else: QtWidgets.QMessageBox.warning(self, "easyPing", "IP地址错误") self.ui.startIP.setFocus() self.ui.startIP.selectAll() def reset_ui(self): ''' 初始化窗口IP窗格为灰色背景 ''' for item in self.ui.label_list: item.setStyleSheet("background-color: rgb(203, 203, 203);") def set_ui(self, result, ip): ''' 设置窗口颜色 result: 线程ping的结果 ip: 为对应的IP地址 ''' index = int(ip.split('.')[3]) if result: self.ui.label_list[index].setStyleSheet("background-color: rgb(85, 170, 127);") # 设置背景为绿色 else: self.ui.label_list[index].setStyleSheet("background-color: rgb(255, 142, 119);") # 设置背景为红色 async def get_ping_result(self, ip): ''' 检查对应的IP是否被占用 ''' try: await aioping.ping(ip, timeout=0.6) self._ping_signal.emit(True, ip) except TimeoutError: self._ping_signal.emit(False, ip) async def start_ping(self): ''' 启动协程 ''' self.reset_ui() startip = self.ui.startIP.text().split('.') endip = self.ui.endIP.text().split('.') tmp_ip = startip tasks = [] for i in range(int(startip[3]), int(endip[3]) + 1): tmp_ip[3] = str(i) ip = '.'.join(tmp_ip) tasks.append(asyncio.create_task(self.get_ping_result(ip))) await asyncio.gather(*tasks) def run(self): asyncio.run(self.start_ping()) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) easy = EasyPing() easy.show() sys.exit(app.exec_())
MyPing.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'MyPing.ui' # # Created by: PyQt5 UI code generator 5.8.1 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtWidgets, QtGui class Ui_MyPing(object): def setupUi(self, MyPing): MyPing.setObjectName("MyPing") MyPing.setWindowIcon(QtGui.QIcon("ping.ico")) # MyPing.resize(660, 385) MyPing.setMaximumSize(QtCore.QSize(660, 425)) MyPing.setMinimumSize(QtCore.QSize(660, 425)) self.groupBox = QtWidgets.QGroupBox(MyPing) self.groupBox.setGeometry(QtCore.QRect(10, 10, 470, 50)) self.groupBox.setObjectName("groupBox") self.widget = QtWidgets.QWidget(self.groupBox) self.widget.setGeometry(QtCore.QRect(10, 20, 441, 25)) self.widget.setObjectName("widget") self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setSpacing(30) self.horizontalLayout.setObjectName("horizontalLayout") self.startIP = QtWidgets.QLineEdit(self.widget) self.startIP.setText("10.153.61.0") self.startIP.selectAll() self.startIP.setObjectName("startIP") self.horizontalLayout.addWidget(self.startIP) self.label_2 = QtWidgets.QLabel(self.widget) self.label_2.setObjectName("label_2") self.horizontalLayout.addWidget(self.label_2) self.endIP = QtWidgets.QLineEdit(self.widget) self.endIP.setObjectName("endIP") self.horizontalLayout.addWidget(self.endIP) self.pingButton = QtWidgets.QPushButton(self.widget) self.pingButton.setObjectName("pingButton") self.horizontalLayout.addWidget(self.pingButton) self.widget1 = QtWidgets.QWidget(MyPing) self.widget1.setGeometry(QtCore.QRect(10, 70, 630, 345)) self.widget1.setObjectName("widget1") self.gridlayout = QtWidgets.QGridLayout(self.widget1) self.gridlayout.setContentsMargins(0, 0, 0, 0) self.gridlayout.setObjectName("gridlayout") self.gridlayout.setSpacing(7) self.label_list = [] list_index = 0 for i in range(1, 17): for j in range(1, 17): label = QtWidgets.QLabel(self.widget1) label.setMinimumSize(QtCore.QSize(32, 15)) label.setStyleSheet("background-color: rgb(203, 203, 203);") label.setAlignment(QtCore.Qt.AlignCenter) label.setText(QtCore.QCoreApplication.translate("MyPing", str(list_index))) self.label_list.append(label) self.gridlayout.addWidget(label, i-1, j-1, 1, 1) list_index += 1 self.retranslateUi(MyPing) QtCore.QMetaObject.connectSlotsByName(MyPing) def retranslateUi(self, MyPing): _translate = QtCore.QCoreApplication.translate MyPing.setWindowTitle(_translate("MyPing", "MyPing")) self.groupBox.setTitle(_translate("MyPing", "Set IP Range")) self.label_2.setText(_translate("MyPing", "——")) self.pingButton.setText(_translate("MyPing", "Ping")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MyPing = QtWidgets.QWidget() ui = Ui_MyPing() ui.setupUi(MyPing) MyPing.show() sys.exit(app.exec_())
ping.ico