在编译的python exe中使用tshark

发布于 2025-01-23 11:05:50 字数 1068 浏览 3 评论 0原文

我有一个工作的Python脚本,它使用和导入Pyshark,因此可以使用Tshark。

只要我通过Pycharm运行代码,一切都很好。一旦我使用pyinstaller编译,我就会遇到此错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "tkinter\__init__.py", line 1921, in __call__
  File "main.py", line 458, in get_pdu_ids
  File "pyshark\capture\capture.py", line 232, in _packets_from_tshark_sync
  File "asyncio\base_events.py", line 646, in run_until_complete
  File "pyshark\capture\capture.py", line 396, in _get_tshark_process
  File "pyshark\capture\capture.py", line 373, in _get_tshark_path
  File "pyshark\tshark\tshark.py", line 30, in get_process_path
  File "configparser.py", line 782, in get
  File "configparser.py", line 1153, in _unify_values
configparser.NoSectionError: No section: 'tshark'

我尝试使用此修复程序,现在应该将其合并到Pyshark中,但是它没有更改任何内容: https://github.com/kiminewt/pyshark/issues/245

当然,一切都是最新的。

注意:其他所有导入的模块可完美地工作,例如Scapy。

提前致谢!

I have a working python script, which uses and imports pyshark, and therefore tshark.

As long as I run the code via pycharm, everything is fine. As soon as I use pyinstaller to compile, I get this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "tkinter\__init__.py", line 1921, in __call__
  File "main.py", line 458, in get_pdu_ids
  File "pyshark\capture\capture.py", line 232, in _packets_from_tshark_sync
  File "asyncio\base_events.py", line 646, in run_until_complete
  File "pyshark\capture\capture.py", line 396, in _get_tshark_process
  File "pyshark\capture\capture.py", line 373, in _get_tshark_path
  File "pyshark\tshark\tshark.py", line 30, in get_process_path
  File "configparser.py", line 782, in get
  File "configparser.py", line 1153, in _unify_values
configparser.NoSectionError: No section: 'tshark'

I tried to use this fix, which should as well be merged into pyshark by now, but it didn`t change anything: https://github.com/KimiNewt/pyshark/issues/245

Of course everything is up-to-date.

Note: Every other imported module works flawlessly, e.g. scapy.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如痴如狂 2025-01-30 11:05:50

尽管这个问题已经回答,但我也有一个类似的问题,我想分享我的解决方案。

Pyshark似乎会在当前目录中加载任何名为“ config.ini”的文件,就好像它是tshark配置文件一样。从get_process_path文档中:

"""Finds the path of the tshark executable.

If the user has provided a path
or specified a location in config.ini it will be used. Otherwise default
locations will be searched.

:param tshark_path: Path of the tshark binary
:raises TSharkNotFoundException in case TShark is not found in any 
location.
"""

的确,如果我们查看pyshark模块中的config.py,该pyshark模块被称为查找tshark目录,我们可以找到:

fp_config_path = Path.cwd() / 'config.ini'  # get config from the current 
directory
pyshark_config_path = Path(pyshark.__file__).parent / 'config.ini'


def get_config():
    if fp_config_path.exists():
        config_path = fp_config_path
    elif pyshark_config_path.exists():
        config_path = pyshark_config_path
    else:
        return None

解决方案是重命名您的配置文件,请将其移动到子目录中,或者包括一个子目录或包括您的配置文件中的tshark和dumpcap部分如下:

[tshark]
# Specify the path to the tshark executable.
# If the configured path does not exist, these locations will be searched:
# (Linux): /usr/bin/tshark
# (Linux): /usr/sbin/tshark
# (Linux): /usr/lib/tshark/tshark
# (Linux): /usr/local/bin/tshark
# (Windows): %ProgramFiles%\Wireshark\tshark.exe
# (Windows): %ProgramFiles(x86)%\Wireshark\tshark.exe
tshark_path = C:\Program Files\Wireshark\tshark.exe

[dumpcap]
dumpcap_path = C:\Program Files\Wireshark\dumpcap.exe

Although this question has been answered, I have had a similar issue and I would like to share my solution.

It seems that pyshark will load any file named 'config.ini' in the current directory as if it were the tshark configuration file. From the get_process_path documentation:

"""Finds the path of the tshark executable.

If the user has provided a path
or specified a location in config.ini it will be used. Otherwise default
locations will be searched.

:param tshark_path: Path of the tshark binary
:raises TSharkNotFoundException in case TShark is not found in any 
location.
"""

Indeed, if we take a look at config.py in the pyshark module that is called to find the tshark directory we can find :

fp_config_path = Path.cwd() / 'config.ini'  # get config from the current 
directory
pyshark_config_path = Path(pyshark.__file__).parent / 'config.ini'


def get_config():
    if fp_config_path.exists():
        config_path = fp_config_path
    elif pyshark_config_path.exists():
        config_path = pyshark_config_path
    else:
        return None

The solution is to either rename your configuration file, move it to a subdirectory, or include the tshark and dumpcap sections in your config file like this:

[tshark]
# Specify the path to the tshark executable.
# If the configured path does not exist, these locations will be searched:
# (Linux): /usr/bin/tshark
# (Linux): /usr/sbin/tshark
# (Linux): /usr/lib/tshark/tshark
# (Linux): /usr/local/bin/tshark
# (Windows): %ProgramFiles%\Wireshark\tshark.exe
# (Windows): %ProgramFiles(x86)%\Wireshark\tshark.exe
tshark_path = C:\Program Files\Wireshark\tshark.exe

[dumpcap]
dumpcap_path = C:\Program Files\Wireshark\dumpcap.exe
神经暖 2025-01-30 11:05:50

我找到了解决方案,在此处发布该解决方案以帮助如果其他人遇到相同的错误:

我必须对以下内容进行硬编码,因为编译的程序没有正确的变量(不知道为什么):

file:\ venv \ lib \ \站点包\ pyshark \ captution \ captut.py

def _get_tshark_path(self):
     return get_process_path(self.tshark_path)

def _get_tshark_path(self):
     return r"C:/Program Files/Wireshark/tshark.exe"

一个肮脏的解决方案,不是解决问题,而是绕过问题,但它有效,但我还是想分享它。

I found the solution, posting it here to help if others get the same error:

I had to hard-code the following, as the compiled program didn`t get the variable correct (no idea why):

File: \venv\Lib\site-packages\pyshark\capture\capture.py

def _get_tshark_path(self):
     return get_process_path(self.tshark_path)

to

def _get_tshark_path(self):
     return r"C:/Program Files/Wireshark/tshark.exe"

It is a dirty solution, not solving but bypassing the problem, but it works and I wanted to share it nonetheless.

反差帅 2025-01-30 11:05:50

尝试:
pyinstaller -collect -all pyshark -f main.py

这应该解决问题

Try:
pyinstaller --collect-all pyshark -F Main.py

This should resolve the issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文