Search code examples
pcapdpkt

'ValueError' object has no attribute '_render_traceback_'


While using dpkt to parser a UDP pcap file, got the following error message:

with open('file.pcap', 'rb') as fopen:
    pcap = dpkt.pcap.Reader(fopen)
for timestamp, buf in pcap:
    print (timestamp)

ERROR:root:Internal Python error in the inspect module. Below is the traceback from this internal error.

Traceback (most recent call last): ValueError: read of closed file

During handling of the above exception, another exception occurred:

Traceback (most recent call last): AttributeError: 'ValueError' object has no attribute 'render_traceback'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): AssertionError


Solution

  • The file is automatically closed when leaving the with open(...) ... block:

    with open('file.pcap', 'rb') as fopen:
        # still open here
        pcap = dpkt.pcap.Reader(fopen)
        
    # automatically closed here
    for timestamp, buf in pcap:
        print (timestamp)
    

    Thus, you need to put your pcap reading into the same block where the file was opened:

    with open('file.pcap', 'rb') as fopen:
        pcap = dpkt.pcap.Reader(fopen)
        for timestamp, buf in pcap:
            print (timestamp)