我正在尝试将 assoc 数组索引设置为 IP,以便我可以跟踪它们在数据包中出现的次数。
log = {}
if log[str(s_addr)] < 1:
log[str(s_addr)] = 1
else:
log[str(s_addr)] += 1
if log[str(s_addr)] > 100:
data_salary = {
'protocol' : str(protocol),
'source_ip': str(s_addr),
'dest_ip': str(d_addr),
'source_port':str(source_port),
'dest_port': str(dest_port)
}
cursor.execute(add_salary, data_salary)
cnx.commit()
print("Logged")
然而,这给了我一个 KeyError
有没有更好的方法来做到这一点?