我不想设置count参数,请修改代码
时间: 2024-03-20 12:38:48 浏览: 16
如果你不想设置 `count` 参数,可以使用 `while` 循环来持续抓包,直到手动停止程序。例如:
```python
import threading
from scapy.all import *
class SnifferThread(threading.Thread):
def __init__(self):
super().__init__()
self.stop_flag = False
def run(self):
while not self.stop_flag:
sniff(filter="icmp", prn=lambda x: x.summary())
def stop(self):
self.stop_flag = True
if __name__ == "__main__":
sniffer_thread = SnifferThread()
sniffer_thread.start()
input("Press Enter to stop...")
sniffer_thread.stop()
sniffer_thread.join()
```
在上面的代码中,`SnifferThread` 类的 `run` 方法中使用 `while` 循环来持续抓取 ICMP 数据包,直到 `stop_flag` 为 `True` 时退出循环。在主线程中,创建 `SnifferThread` 对象并启动它,然后使用 `input` 函数等待用户按下回车键停止程序,此时调用 `stop` 方法停止线程,并调用 `join` 等待线程结束。这样可以实现持续抓包的功能,直到手动停止程序为止。