PyQt4和Scapy中的PingTool

2024-09-30 08:25:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着用Scapy和PyQt4做一个小ping工具。 代码相当简单,它现在所做的就是ping用户可以输入的地址。在

from PyQt4 import QtGui
import sys
from scapy.all import *
from scapy.sendrecv import sr, send

def q2s(qstr): return "%s" %qstr

class Application(QtGui.QMainWindow):

    def __init__(self):
        super(Application, self).__init__()
        self.resize(1000,500)
        self.centre()

        self.initGui()
        self.show()

    def initGui(self):

        self.ipAddress = QtGui.QLineEdit("1.1.1.1",self)

        self.label = QtGui.QLabel("...")
        self.label.move(50,100)
        pingBtn = QtGui.QPushButton("Ping!", self)
        pingBtn.move(50,50)

        pingBtn.clicked.connect(self.ping)

    def ping(self):
        ip = q2s(self.ipAddress.text())

        ans, unans = sr(IP(dst=ip)/ICMP(), timeout=1, verbose=0)
        if ans:
            self.label.setText("Host is up")
        else:
            self.label.setText("Host is down")

    def centre(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        sizeNow = self.geometry()
        self.move((screen.width() - sizeNow.width()) / 2, 
                  (screen.height() - sizeNow.height()) / 2)

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Application()
    sys.exit(app.exec_())

run()

但是,当尝试ping一个IP地址时,会向控制台输出一个错误。在

^{pr2}$

最后一行的意思是“操作系统调用期间中断”。在

我看不出程序有什么问题。在

使用send函数而不是sr函数是可行的。所以我认为问题可能是应用程序正在等待响应。但我还是不知道如何修正这个错误。在


Tags: fromimportselfmoveapplicationdefsysping
1条回答
网友
1楼 · 发布于 2024-09-30 08:25:21

这是Scapy中的一个bug:在多线程环境中,调用select()时要捕获的异常是不同的。在

一个拉请求(#417)目前正在检查(合并后我会更新这个答案),因此您可以检查patch是否为您解决了这个问题(我希望如此!)。在

相关问题 更多 >

    热门问题