Reputation: 44395
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
?
Upvotes: -1
Views: 281
Reputation: 44395
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:
capture = pyshark.FileCapture(filename)
try:
for packet in capture:
...
except pyshark.capture.capture.TSharkCrashException:
pass
try:
capture.close()
except pyshark.capture.capture.TSharkCrashException:
pass
and then at the very end of your code you have to redirect the stderr
to /dev/null
:
devnull = open(os.devnull, 'w')
sys.stderr = devnull
Not sure what is going on, but it seems to work!
Upvotes: 0
Reputation: 318
according to the issue:
https://github.com/KimiNewt/pyshark/issues/339
and the source code of pyshark
latest version, in here https://github.com/KimiNewt/pyshark/blob/master/src/pyshark/capture/capture.py
line 366
in _cleanup_subprocess
method the code looks like this:
async def _cleanup_subprocess(self, process):
"""Kill the given process and properly closes any pipes connected to it."""
self._log.debug(f"Cleanup Subprocess (pid {process.pid})")
if process.returncode is None:
try:
process.kill()
return await asyncio.wait_for(process.wait(), 1)
except asyncTimeoutError:
self._log.debug(
"Waiting for process to close failed, may have zombie process.")
except ProcessLookupError:
pass
except OSError:
if os.name != "nt":
raise
elif process.returncode > 0:
if process.returncode != 1 or self._eof_reached:
raise TSharkCrashException(f"TShark (pid {process.pid}) seems to have crashed (retcode: {process.returncode}).\n"
f"Last error line: {self._last_error_line}\n"
"Try rerunning in debug mode [ capture_obj.set_debug() ] or try updating tshark.")
what I think is that you are handling the Exception
because you are printing the exception
in your terminal
this Exception
that raises and you didn't catch it as occurring after that
try to closing your capture
with:
capture = pyshark.FileCapture(filename)
try:
for packet in capture:
pass
except:
print("exception")
capture.close()
Upvotes: 0