wxPython:停止使用wxButton线程

2024-05-19 00:40:05 发布

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

如何通过单击wx按钮停止线程?在

这是我的代码:

def startMonitor(self,event):       
    selectedInterface = self.interfaces_cblist.GetValue()
    Publisher().sendMessage(("test"),selectedInterface) 
    self.Close()
    selectInterfaceStr = str(selectedInterface) 
    if len(selectedInterface) == 0:
        noSelect_error = wx.MessageDialog(None,"Please select an interface","",wx.OK|wx.ICON_ERROR)
        noSelect_error.ShowModal()
    else:       
        monitorStarted = wx.MessageDialog(None,"Monitor on %s started"%selectInterfaceStr,"",wx.OK|wx.ICON_ERROR)
        monitorStarted.ShowModal()
        self.monitorInterface_button.Disable()      
        threading.Thread(target=self.camtableDetection,args=(selectInterfaceStr,)).start()
        threading.Thread(target=self.dhcpexhaustion,args=(selectInterfaceStr,)).start()

def camtableDetection(self,getInterface):
        global interface        
        interface = str(getInterface)
        THRESH=(254/4)
        START = 5
        def monitorPackets(p):
            if p.haslayer(IP):
                hwSrc = p.getlayer(Ether).src
                if hwSrc not in hwList:
                    hwList.append(hwSrc)
                delta = datetime.datetime.now() - start
                if((delta.seconds > START) and ((len(hwList)/delta.seconds) > THRESH)):
                    print "[*]- Detected CAM Table Attack."
                    #camAttackDetected = wx.MessageDialog(None,"Cam Attack Detected","",wx.ICON_ERROR)
                    #camAttackDetected.ShowModal()

    hwList = []
    start = datetime.datetime.now()
    sniff(iface=interface,prn=monitorPackets)


def dhcpexhaustion(self,getInterface):
    interface = str(getInterface)
    global reqCnt
    global ofrCnt
    reqCnt = 0
    ofrCnt = 0

    def monitorPackets(p):
        if p.haslayer(BOOTP):
            global reqCnt
            global ofrCnt
            opCode = p.getlayer(BOOTP).op
            if opCode == 1:
                reqCnt=reqCnt+1
            elif opCode == 2:
                ofrCnt=ofrCnt+1
            print "[*] - "+str(reqCnt)+" Requests, "+str(ofrCnt)+" Offers."
          sniff(iface=interface,prn=monitorPackets)

当我点击一个按钮时,我想停止线程,但不知道怎么做。在

有自行中止技术,但我不知道如何在代码中应用它。在


Tags: selfifdefglobalstartinterfacewxstr
1条回答
网友
1楼 · 发布于 2024-05-19 00:40:05

正如我在评论中所说:

If [sniff is] a function that you have no control over (e.g., from a C extension module) and it loops forever, then it must have some way to cancel it. Maybe it's having your callback return a special value, maybe it's calling a control function, maybe it's closing the object it's working on… whatever it is, you have to do that.

那么,为什么不阅读scapy.sniff的文档,看看如何取消它呢?在

Sniff packets
sniff([count=0,] [prn=None,] [store=1,] [offline=None,] [lfilter=None,] + L2ListenSocket args) -> list of packets

  count: number of packets to capture. 0 means infinity
  store: wether to store sniffed packets or discard them
    prn: function to apply to each packet. If something is returned,
         it is displayed. Ex:
         ex: prn = lambda x: x.summary()
lfilter: python function applied to each packet to determine
         if further action may be done
         ex: lfilter = lambda x: x.haslayer(Padding)
offline: pcap file to read packets from, instead of sniffing them
timeout: stop sniffing after a given time (default: None)
L2socket: use the provided L2socket
opened_socket: provide an object ready to use .recv() on
stop_filter: python function applied to each packet to determine
             if we have to stop the capture after this packet
             ex: stop_filter = lambda x: x.haslayer(TCP)

所以,阻止它永远嗅探的方法是向它传递一个stop_filter函数,当您想停止它时,它将返回True。所以这个函数就是检查stop标志的地方。例如:

^{pr2}$

您可能希望在开始时存储这两个Thread对象,这样您就可以在停止时间join它们,而不是在程序退出之前泄漏它们。但不然,这应该行得通。在

相关问题 更多 >

    热门问题