Running the following python 3.10.12 code on Ubuntu 22.04.4 using pyshark
0.6 seems to ignore the try-except
statement of python:
capture = pyshark.FileCapture(filename)
try:
for packet in capture:
pass
except:
print("exception")
Even I enclosed the reading loop in a try-except
statement I get the following output:
exception
Exception ignored in: <function Capture.__del__ at 0x79b5be57c3a0>
Traceback (most recent call last):
File "/home/alex/.local/lib/python3.10/site-packages/pyshark/capture/capture.py", line 405, in __del__
self.close()
File "/home/alex/.local/lib/python3.10/site-packages/pyshark/capture/capture.py", line 393, in close
self.eventloop.run_until_complete(self.close_async())
File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
return future.result()
File "/home/alex/.local/lib/python3.10/site-packages/pyshark/capture/capture.py", line 397, in close_async
await self._cleanup_subprocess(process)
File "/home/alex/.local/lib/python3.10/site-packages/pyshark/capture/capture.py", line 379, in _cleanup_subprocess
raise TSharkCrashException(f"TShark (pid {process.pid}) seems to have crashed (retcode: {process.returncode}).n"
pyshark.capture.capture.TSharkCrashException: TShark (pid 8097) seems to have crashed (retcode: 2).
Last error line: tshark: The file "/home/alex/Work/Data/test.pcap" appears to have been cut short in the middle of a packet.
Try rerunning in debug mode [ capture_obj.set_debug() ] or try updating tshark.
How to suppress this output?
To clarify: I am not interested to fix any problem or error. I am just interested into suppressing this output of the exception!
Maybe there is a way to have pyshark
ignore errors?
Or I can redirect all error output from pyshark
?
2
Answers
I think I found a way to suppress this output. First, you have to put the loop and the close statement into a
try-except
statement:and then at the very end of your code you have to redirect the
stderr
to/dev/null
:Not sure what is going on, but it seems to work!
according to the
issue:
https://github.com/KimiNewt/pyshark/issues/339and the source code of
pyshark
latest version, in here https://github.com/KimiNewt/pyshark/blob/master/src/pyshark/capture/capture.pyline
366
in_cleanup_subprocess
method the code looks like this:what I think is that you are handling the
Exception
because you are printing theexception
in your terminalthis
Exception
that raises and you didn’t catch it as occurring after thattry to closing your
capture
with: