简体   繁体   English

附加字典时出现KeyError

[英]KeyError while appending dictionary

I am getting a key error while trying to add a variable from a parsed pcap file.尝试从已解析的 pcap 文件中添加变量时出现关键错误。

Here I am trying to get the last time stamp but I get a key error在这里,我试图获取最后一个时间戳,但我得到一个关键错误

KeyError: 'UPD'


lst_timestamp = {}

for ts, buf in packets:
    eth = dpkt.ethernet.Ethernet(buf)
    ip = eth.data
    protocol_number = ip.p
    traffic_type = protocoltype.proto[protocol_number]
    if traffic_type not in traffic_types:
        traffic_types.append(traffic_type)

    if traffic_type in no_packets: # check the key is in the dictionary
        no_packets[traffic_type] += 1
    else:
        no_packets[traffic_type] = 1
        
    if traffic_type not in fst_timestamp:
        fst_timestamp[traffic_type] = ts
    
    if traffic_type in lst_timestamp:
        lst_timestamp[traffic_type] = ts

The error is occurring here:错误发生在这里:

if traffic_type in lst_timestamp:
    lst_timestamp[traffic_type] = ts

I am fairly new to python so I have tried to find why this error occurs but I can't figure it out, any help would be much apricated.我对 python 相当陌生,所以我试图找出为什么会发生此错误,但我无法弄清楚,任何帮助都会非常有用。

Full code.完整的代码。

import dpkt
import socket
from tabulate import tabulate
import protocoltype

def main() -> list:    
    """parse pcap"""
    pcap_file = "evidence-packet-analysis.pcap"

    packets = []
    open_file = open(pcap_file, "rb")    
    for ts, buf in dpkt.pcap.Reader(open_file):
        packets.append((ts, buf))
    open_file.close()

    print(f"'{pcap_file}' Successfully Read")

    table(packets)

def table(packets):
    traffic_types = []
    no_packets = {}
    fst_timestamp = {}
    lst_timestamp = {}

    for ts, buf in packets:
        eth = dpkt.ethernet.Ethernet(buf)
        ip = eth.data
        protocol_number = ip.p
        traffic_type = protocoltype.proto[protocol_number]
        if traffic_type not in traffic_types:
            traffic_types.append(traffic_type)

        if traffic_type in no_packets: # check the key is in 
the dictionary
            no_packets[traffic_type] += 1
        else:
            no_packets[traffic_type] = 1
        
        if traffic_type not in fst_timestamp:
            fst_timestamp[traffic_type] = ts
    
        if traffic_type in lst_timestamp:
            lst_timestamp[traffic_type] = ts
        
    rows = []
    for traffic_type in traffic_types:
        rows.append([traffic_type, no_packets[traffic_type], 
fst_timestamp[traffic_type], lst_timestamp[traffic_type], 0])

    print(tabulate(
        rows,
        headers=["traffic_type", "no_packets", "fst_timestamp", 
"lst_timestamp", "mean_packet_length"]
    ))


if __name__ == "__main__" :
    main()

The module protocoltype is just a dictionary linking protocol numbers to protocol type模块协议类型只是一个将协议编号链接到协议类型的字典

KeyError: 'UPD'键错误:'UPD'

Ok where's the 'UPD'?好的,“UPD”在哪里?

There's no 'UPD' key in your code您的代码中没有“UPD”键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM