easyPing工具
目录结构
easyPing.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 | # -*- 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
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 | # -*- 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
本文作者:香菜哥哥
本文链接:https://www.cnblogs.com/yizhipanghu/p/17541513.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
标签:
easyPing
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2019-07-10 如何在思科交换机上配置Telnet远程登录