Scapy stop_filter导致巨大的内存泄漏

问题描述 投票:0回答:1

我有一个启动虚拟主机的应用程序。我注意到记忆力正在迅速增加。经过多次试图找到原因的试验,结果证明原因是在stop_filter中使用scapy

以下简化代码是可运行的,您只需复制/粘贴:

from scapy.all import *
import threading
from time import sleep

def stopFilter(packet):
    if ICMP in packet:
        if packet[1].dst == '192.168.0.70':
            print('packet found')
            return True
def host():
    while True:
        sniff(iface="Intel(R) PRO/1000 PT Dual Port Server Adapter #2", timeout=2, stop_filter=stopFilter, store=0)
        sleep(2)

for i in range(200):
    print(i)
    t = threading.Thread(target=host)
    t.start()
    sleep(0.1)

当然,您需要更改适配器和IP。此外,在运行代码时使用ping -t到IP,以便stopFilter()工作。片刻之后,您可以看到内存正在积聚。 I think similar issue in C with libpcap.

不知道怎么解决这个问题?

环境:Python 3.6.0,Win 7,Scapy 2.4.0(Scapy 2.4.2中的相同问题)

python python-3.x memory-leaks scapy libpcap
1个回答
0
投票

我自己解决了这个问题。只需前往~Lib\site-packages\scapy\sendrecv.py及其下

if stop_filter and stop_filter(p):取代:

sniff_sockets = []
break

有:

for s in sniff_sockets:
     s.close()
del sniff_sockets
return   

现在记忆问题消失了。

更新:

上面提到的解决方案,仅在2.4.0(无内存泄漏)中有所帮助,但在2.4.1和2.4.2中没有帮助

© www.soinside.com 2019 - 2024. All rights reserved.