[英]Tkinter window freezes when sniffing with scapy
import tkinter as tk
import random
import scapy.all as scapy
result = ""
def getmac(ip):
arplayer = scapy.ARP(pdst=ip)
etherlayer = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
packet = etherlayer/arplayer
answeredList = scapy.srp(packet, timeout=2, verbose = False)[0]
return answeredList[0][1].hwsrc
def sniff():
scapy.sniff(store=False, prn=checkattack)
def checkattack(packet):
try:
if packet.haslayer(scapy.ARP) and packet[scapy.ARP].op == 2:
realmac = getmac(packet[scapy.ARP].psrc)
rspmac = packet[scapy.ARP].hwsrc
if realmac != rspmac:
result = "WARNING"
except IndexError:
pass
GUI = tk.Tk()
GUI.title("NETWORK GUARDIAN")
GUI.geometry("600x500")
# === labels ===
label1 = tk.Label(text="> Welcome to the NETWORK GUARDIAN !", font=("Times", 11))
label1.grid(column = 0, row = 0)
label2 = tk.Label(text = result, font=("Arial Black", 15))
label2.grid(column = 0, row = 3)
# === buttons ===
button1 = tk.Button(text="Scan for attacks", command = sniff)
button1.grid(column = 0, row = 2)
GUI.mainloop()
I have this arp spoofing tool made with scapy, but I am trying to display the results in a nicely way by using tkinter. 我有这个用scapy制作的arp欺骗工具,但是我试图通过使用tkinter很好地显示结果。 The problem is that when I press the button to scan for possible attacks the window freezes.
问题是当我按下按钮扫描可能的攻击时,窗口冻结。 I guess this happens because the sniff() function is a long-running function.
我猜这是因为sniff()函数是一个长期运行的函数。 Can someone help me to make this work the right way?
有人可以帮助我以正确的方式进行这项工作吗?
Maybe you could try the threading module to keep it from interrupting the tkinter loop? 也许您可以尝试使用线程模块来防止其中断tkinter循环? The syntax is something like this
语法是这样的
import threading
sniffThread = threading.thread(target=sniff)
sniffThread.run()
sniffThread.stop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.