1

I'm getting this error which I haven't been able to figure out for a long time in python:

Traceback (most recent call last):  
  File "st2110_parse_KB.py", line 173, in <module>  
    section_header = get_pcapng_section_header(input_pcapng_file)  
  File "Z:\easy_parse\pcapng.py", line 190, in get_pcapng_section_header  
    assert(block_types[block_type] == "Section Header Block"), \  
KeyError: 3569595041L

I'm calling the function get_pcapng_section_header() which is in another file that I imported like so:

from easy_parse.pcapng import *

Here is how I'm calling the module from my main function:

#  input_pcapng_file = open("filename.pcap",'rb')

if filename:
  print "file works"
else:
  print "doesnt"

port_filter_str + '.payload'


print "its fine here"

section_header = get_pcapng_section_header(input_pcapng_file)

Here's a snippet of the pcapng module:

def get_pcapng_section_header(input_file):

  # The Section Header Block is special because it defines the endian.
  # Because of this get_pcapng_block (which requires endian) won't be used
  # until the endian is determined.

  section_header_block_file_position = input_file.tell()

  # The endian doesn't matter for the Section Header Block's Block Type.
  block_type = bytes_to_int(input_file.read(4), "big")

  assert(block_types[block_type] == "Section Header Block"), \
      "Block Type %d is not Section Header Block at 0x%08x" % (block_type, section_header_block_file_position)

  # Skip the length for now.
  input_file.read(4)
tehhowch
  • 9,645
  • 4
  • 24
  • 42
Ken
  • 163
  • 2
  • 11
  • Without the relevant `pcapgn` module code, we can't help. – Martijn Pieters Mar 06 '18 at 18:27
  • Usually KeyErrors are not inexplicable, they are a result of trying to access a key that does not exist. Have you tried to [Catch the error](https://docs.python.org/3/tutorial/errors.html#handling-exceptions) and inspect/print relevant data in the except suite? – wwii Mar 06 '18 at 18:27
  • Hey so i updated the code – Ken Mar 06 '18 at 18:33
  • and yes @wwii I also tried to print at certain points – Ken Mar 06 '18 at 18:38
  • could you please put it off hold @MartijnPieters – Ken Mar 06 '18 at 19:35
  • 1
    You call your code with commented-out input file? Are you sure the code written in your post is a minimal, complete, verifiable example (MCVE)? What is `filename`? You don't define it in the code you included. What does `filename.pcap` look like, i.e. include a few lines from the start of the file? Do you get the `KeyError` for just that particular input file, or any file? There's a lot more you can do to make this question easier to re-open. – tehhowch Mar 06 '18 at 19:50
  • Could you provide the file in question? It sounds like you are trying to open a pcap file using code that can only handle pcapng. – Ayoub Kaanich Oct 10 '20 at 20:18

0 Answers0