gpt4 book ai didi

python-2.7 - Scapy 以非阻塞方式嗅探

转载 作者:行者123 更新时间:2023-12-04 08:55:35 24 4
gpt4 key购买 nike

以阻塞方式,我可以这样做:

from scapy.all import *

sniff(filter"tcp and port 80", count=10, prn = labmda x:x.summary())
# Below code will be executed only after 10 packets have been received
do_stuff()
do_stuff2()
do_stuff3()

我希望能够以非阻塞方式使用 scapy 嗅探数据包,如下所示:
def packet_recevied_event(p):
print "Packet received event!"
print p.summary()

# The "event_handler" parameter is my wishful thinking
sniff(filter"tcp and port 80", count=10, prn=labmda x:x.summary(),
event_handler=packet_received_event)

#I want this to be executed immediately
do_stuff()
do_stuff2()
do_stuff3()

总结一下:我的问题很清楚,我希望能够继续执行代码而不会被嗅探函数阻塞。
一种选择是为此打开一个单独的线程,但我想避免它并尽可能使用 scapy native 工具。

环境细节:

python :2.7

scapy:2.1.0

操作系统:ubuntu 12.04 64 位

最佳答案

此功能是在 https://github.com/secdev/scapy/pull/1999 中添加的.
我将提供 Scapy 2.4.3+(或 github 分支)。看一下文档:https://scapy.readthedocs.io/en/latest/usage.html#asynchronous-sniffing

>>> t = AsyncSniffer(prn=lambda x: x.summary(), store=False, filter="tcp")
>>> t.start()
>>> time.sleep(20)
>>> t.stop()

关于python-2.7 - Scapy 以非阻塞方式嗅探,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19929326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com