python - PyQt4 和 Scapy 中的 Ping 工具

标签 python user-interface pyqt4 scapy

我正在尝试使用 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 地址时,控制台会打印出一个错误。

Traceback (most recent call last):
  File "Application.py", line 71, in ping
    ans, unans = sr(IP(dst=ip)/ICMP(), timeout=1, verbose=0)
  File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 317, in sr
    a,b=sndrcv(s,x,*args,**kargs)
  File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 129, in sndrcv
    inp, out, err = select(inmask,[],[], remaintime)
select.error: (4, 'Unterbrechung w\xc3\xa4hrend des Betriebssystemaufrufs')

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

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

使用 send 函数而不是 sr 函数以某种方式工作。所以我认为问题可能是应用程序正在等待响应。但我仍然不知道如何修复错误。

最佳答案

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

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

关于python - PyQt4 和 Scapy 中的 Ping 工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32905560/

相关文章:

python - 如何解决Graphene-django中的 'NoneType'对象没有属性 'fields'

java - 如何在 JFrame 中的 JPanel 之间切换

c++ - 我如何在 Qt 中的小部件下绘制阴影?

pycharm - 似乎无法在简单的 PyQt 程序中修复 PyCharm 警告

python - 导入错误 : No module named sip (python2. 7 PyQt4)

python - 删除不在引号内的空格

python - 如何实时解决线程死锁?

user-interface - 为什么模态/非模态对话框称为模态/非模态?

python - 在 PyQT 中获取布局的小部件

python - 当 type(self) 工作正常时,为什么 Loan 未定义?