问题描述
在更新变量方面,我遇到了一个不寻常的问题.我建立了一个简单的类对象来帮助我进行网络嗅探.我想做一个并行过程,使我可以运行一些网络测试,并捕获使用python生成的流量,以便我可以扩展程序以完成令人惊奇的事情.我正在使用scapy的嗅探功能来帮助进行界面嗅探.
I've come across an unusual problem in regards to updating variables. I've built a simple class object to help me with some network sniffing. I wanted to make a parallel process which allows me to run some network tests and capture the traffic generated using python so I can extend the program to do amazing things. I'm using scapy's sniffing function to help with the interface sniffing.
Scapy的嗅探器使您可以将一个函数传递给自身,该函数使您可以创建停止嗅探"条件.就我而言,我已经创建了函数stop_filter
,并且希望通过简单地更新self.stop_sniffing
实例变量来停止Scapy嗅探功能.我在下面介绍了程序输出,该输出显示self.stop_sniffing
在功能stop
中设置为True,但是在stop_filter
中打印时又设置为False(或者根本不更新).我不知道为什么会这样,也没有解决办法,因为这是一个很奇怪的问题.
Scapy's sniffer allows you to pass a function into itself function that allows you to create a 'stop sniffing' condition. In my case I've created function stop_filter
and I wish to stop the Scapy sniff function by simply updating the self.stop_sniffing
instance variable. I've presented the program output below, which shows self.stop_sniffing
getting set to True in Function stop
, but is then set back to False (or is not updated at all) when printed in stop_filter
. I have no clue why this is happening and no solution comes to mind as it's such a weird problem.
如果任何有新鲜眼睛的人都能看到我在这里所做的疯狂的事情,将不胜感激!
If anyone with fresh eyes can see what insane thing I've done here it would be greatly appreciated!
from scapy.all import *
from multiprocessing import Process
class DatasetSniffer:
def __init__(self, iface, local_dir='.'):
self.iface = iface
self.master = None
self.local_dir = local_dir
self.stop_sniffing = False # Never updates! why!?
self.writer = PcapWriter(local_dir+"/master.pcap", append=True, sync=True)
def stop_filter(self, p):
# Note: 'p' gets passed in by Scapy function 'sniff'
print self.stop_sniffing
# Return 'True' to stop sniffer
return self.stop_sniffing
def sniff(self):
sniff(store=0, prn=self.writer.write, iface=self.iface, stop_filter=self.stop_filter)
def start(self):
self.master = Process(target=self.sniff)
self.master.start()
def stop(self):
self.stop_sniffing = True
# Shows that self.stop_sniffing is 'True'
print self.stop_sniffing
self.master.join()
if __name__ == "__main__":
interface = 'en3'
sniffer = DatasetSniffer(interface)
sniffer.start()
# some process
time.sleep(5)
sniffer.stop()
Shell输出:
sudo python sniffing.py
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
True
False
False
False
False
推荐答案
感谢您的所有建议.经过一番启发后,我设法弄清了这个脚本.可能是解决我的问题而不进行太多更改的更好的方法.因此,此代码允许线程在类外部使用stop函数,从而允许所有异步任务使用stop_filter
.
Thanks for all your suggestions. After a glass of inspiration I managed to knock up this script. Probably a nicer way to approach my problem without making too many changes. So this code allows the threads to use the stop function outside the class, thus allowing all the asynchronous tasks to use the stop_filter
.
在下面的链接中找到了此信息.希望这篇文章对其他人有用! http://www.tutorialspoint.com/python/python_multithreading.htm
Found this information in the link below. Hopfully this post will be useful to someone else!http://www.tutorialspoint.com/python/python_multithreading.htm
干杯!
import threading
from scapy.all import *
from datetime import datetime
directory = str(datetime.now().strftime("%Y%m%d%H%M%S"))
os.makedirs(directory)
DatasetSnifferExit = 0
class DatasetSniffer(threading.Thread):
def __init__(self, iface, local_dir='.', filename=str(datetime.now())):
self.iface = iface
self.filename = filename
self.local_dir = local_dir
self.stop_sniffing = False
self.writer = PcapWriter(local_dir+"/"+filename+".pcap", append=True, sync=True)
threading.Thread.__init__(self)
def run(self):
sniff_interface(self.writer.write, self.iface)
def stop_filter(p):
if DatasetSnifferExit:
return True
else:
return False
def sniff_interface(write, iface):
sniff(store=0, prn=write, iface=iface, stop_filter=stop_filter)
if __name__ == "__main__":
DatasetSnifferExit = False
# Create new threads
pcap1 = DatasetSniffer('en3', directory, "master")
pcap2 = DatasetSniffer('en0', directory, "slave")
# Start new Threads
pcap1.start()
pcap2.start()
# Do stuff
time.sleep(10)
# Finished doing stuff
DatasetSnifferExit = True
这篇关于使用多重处理时,实例变量未更新Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!