pyshark icon indicating copy to clipboard operation
pyshark copied to clipboard

cannot read pcapng file which wireshark can

Open chanansh opened this issue 3 years ago • 2 comments

Describe the bug cannot read pcapng file, receiving an error: AttributeError: type object '_asyncio.Task' has no attribute 'all_tasks'

File ~\Miniconda3\envs\ngnddos\lib\site-packages\pyshark\capture\file_capture.py:38, in FileCapture.__init__(self, input_file, keep_packets, display_filter, only_summaries, decryption_key, encryption_type, decode_as, disable_protocol, tshark_path, override_prefs, use_json, use_ek, output_file, include_raw, eventloop, custom_parameters, debug)
     10 def __init__(self, input_file=None, keep_packets=True, display_filter=None, only_summaries=False,
     11              decryption_key=None, encryption_type="wpa-pwk", decode_as=None,
     12              disable_protocol=None, tshark_path=None, override_prefs=None,
     13              use_json=False, use_ek=False,
     14              output_file=None, include_raw=False, eventloop=None, custom_parameters=None,
     15              debug=False):
     16     """Creates a packet capture object by reading from file.
     17 
     18     :param keep_packets: Whether to keep packets after reading them via next(). Used to conserve memory when reading
   (...)
     36     or else a list of parameters in the format ["--foo", "bar", "--baz", "foo"].
     37     """
---> 38     super(FileCapture, self).__init__(display_filter=display_filter, only_summaries=only_summaries,
     39                                       decryption_key=decryption_key, encryption_type=encryption_type,
     40                                       decode_as=decode_as, disable_protocol=disable_protocol,
     41                                       tshark_path=tshark_path, override_prefs=override_prefs,
     42                                       use_json=use_json, use_ek=use_ek, output_file=output_file,
     43                                       include_raw=include_raw, eventloop=eventloop,
     44                                       custom_parameters=custom_parameters, debug=debug)
     45     self.input_filepath = pathlib.Path(input_file)
     46     if not self.input_filepath.exists():

File ~\Miniconda3\envs\ngnddos\lib\site-packages\pyshark\capture\capture.py:88, in Capture.__init__(self, display_filter, only_summaries, eventloop, decryption_key, encryption_type, output_file, decode_as, disable_protocol, tshark_path, override_prefs, capture_filter, use_json, include_raw, use_ek, custom_parameters, debug)
     86 self.eventloop = eventloop
     87 if self.eventloop is None:
---> 88     self._setup_eventloop()
     89 if encryption_type and encryption_type.lower() in self.SUPPORTED_ENCRYPTION_STANDARDS:
     90     self.encryption = (decryption_key, encryption_type.lower())

File ~\Miniconda3\envs\ngnddos\lib\site-packages\pyshark\capture\capture.py:176, in Capture._setup_eventloop(self)
    172     self.eventloop = current_eventloop
    173 else:
    174     # On Python before 3.8, Proactor is not the default eventloop type, so we have to create a new one.
    175     # If there was an existing eventloop this can create issues, since we effectively disable it here.
--> 176     if asyncio.Task.all_tasks():
    177         warnings.warn("The running eventloop has tasks but pyshark must set a new eventloop to continue. "
    178                       "Existing tasks may not run.")
    179     self.eventloop = asyncio.ProactorEventLoop()

AttributeError: type object '_asyncio.Task' has no attribute 'all_tasks'

To Reproduce

import pyshark
capture = pyshark.FileCapture('data/data.pcap')

Expected behavior capture holds packets

Versions (please complete the following information):

  • OS: Windows 10
  • pyshark version: 0.5.3
  • tshark version: 3.6.6

Example pcap / packet If applicable, add an example pcap file as an attachment, or post the packet as a hex string or a JSON/XML (export packet dissection in wireshark/tshark).

chanansh avatar Jul 31 '22 19:07 chanansh

@chanansh facing the same issue right now on Windows only (I can successfully use it on Ubuntu). Did you still have the same issue?

Ruben-Crispino avatar Nov 25 '22 15:11 Ruben-Crispino

Same here

Specs

  • System: Windows 10
  • Python: 3.10.5
  • pyshark: 0.5.3
  • Environment: venv

Origins of the issue

The issues seems to be related to this update.

The asyncio module has received many new features, usability and performance improvements. Notable changes include:

[...] The new asyncio.current_task() function returns the currently running Task instance, and the new asyncio.all_tasks() function returns a set of all existing Task instances in a given loop. The Task.current_task() and Task.all_tasks() methods have been deprecated. (Contributed by Andrew Svetlov in bpo-32250.)

From asyncio doc:

Deprecated since version 3.7, will be removed in version 3.9: Do not call this as a task method. Use the asyncio.all_tasks() function instead.

Working fix

Modified pyshark\capture\capture.py:176 in my local .venv from:

if asyncio.Task.all_tasks():

to

if asyncio.all_tasks():

mattia-lecci avatar Mar 01 '23 13:03 mattia-lecci