我正在尝试使用scapy捕获网络流量,通过在线程中运行sniff而不是在主线程本身中运行它。这样可以避免阻止应用。但我面临的问题是wrpcap没有创建文件,如果它创建了一个0 Kb文件。
我使用单独线程的另一个原因是,因为我希望能够在用户希望结束捕获时关闭线程。
from scapy.all import *
from time import gmtime , strftime
import threading
import time
def bomber(stop_event):
data=[]
pkts=[]
while not stop_event.isSet() :
pkt100=sniff(count=100)
data.append(pkt100)
for pkt in data:
for x in range(100):
pkts.append(pkt[x])
pktsRoll=PacketList(pkts)
savename="F:\\%s.pcap" % strftime("%Y-%m-%d,%H:%M", gmtime())
wrpcap(savename,pktsRoll)
print " its done "
def main():
stop_event = threading.Event()
c_thread = threading.Thread(target=bomber, args=(stop_event,))
c_thread.start()
time.sleep(20)
stop_event.set()
这是我正在尝试的代码。我正在使用Python 2.5
提前致谢。