这段代码前面两行是导入Mytopo.py和construction.py文件,但是在终端运行的时候什么结果都没看见,construction.py的功能是构造数据包并以.pcap文件的形式存在目录w下面,但是我看了w目录发现里面一个构造成功的数据包也没有,所以代码好像从第一行就没运行,有没有好心人知道是怎么回事啊
import Mytopo
import construction
from scapy.all import *
from collections import defaultdict
import os
from scapy.all import rdpcap
class CountMinSketch:
def __init__(self, w, d):
self.w = w
self.d = d
self.tables = [defaultdict(int) for _ in range(w)] # 创建包含w个默认字典的列表,每个字典的默认值为0
def add(self, item): # 添加元素
for table in self.tables: # 遍历每个表
h = hash(item) % (1 << self.d) # 计算哈希值h,取模2^d
table[h] += 1
def estimate(self, item):
min_estimate = float('inf') # 初始化为正无穷大
for table in self.tables:
h = hash(item) % (1 << self.d)
if table[h] < min_estimate:
min_estimate = table[h]
return min_estimate
def process_packet(packet, sketch): # 处理数据包
key = (packet['Source'],packet['Destination'],packet['Src Port'], packet['Dst Port'], packet['Protocol']) # 五元组作为键
sketch.add(key)
return sketch
if __name__ == "__main__":
w = 10
d = 5
sketch = CountMinSketch(w, d)
# 获取目录work下的所有文件名
file_list = os.listdir("w")
packets = []
for file_name in file_list:
if file_name.endswith(".pcap"):
packets_temp=rdpcap(os.path.join("w",file_name))
packets.extend(packets_temp)
for packet in packets:
sketch = process_packet(packet, sketch)
for key in sketch.tables[0]:
print(f"{key}: {sketch.estimate(key)}")