Scapy 不适用于 Python 3.6。 Python 的导入问题?

Scapy not working with Python 3.6. Import problems with Python?

我正在尝试测试将使用 scapy 检查 .pcap 文件的简单代码。我的 OS 是 Ubuntu 18.04,我的 Python 版本是 3.6.

但是当我这样做时:from scapy.all import * 我收到错误消息 ModuleNotFoundError: No module named 'scapy.all'

当我将其更改为 from scapy import * 时,我稍后在尝试使用 scapy sendrecv 嗅探函数时在代码中遇到错误。我收到错误 NameError: name 'sniff' is not defined。请注意,如果我从 sniff() 切换到 rdpcap(),我会得到同样的错误,但它现在“rdpcap 未定义”。

我已经通读了一堆以前的堆栈溢出答案,但没有任何帮助。我的 python 脚本名称是“pcap_grapher.py”,所以问题是 而不是 脚本名称是 scapy.py.

当我在终端中 运行 pip3 install scapy 时,我收到消息 Requirement already satisfied: scapy in /home/vic/.local/lib/python3.6/site-packages (2.4.4)

我也试过 运行ning pip3 install --pre scapy[basic] 作为文档推荐在这里:https://scapy.readthedocs.io/en/latest/installation.html 但它没有帮助。

如有任何建议,我们将不胜感激。我超级卡。

首先,您将永远无法从 scapy 导入 sniff(),因为它位于子模块 scapy.all 中。我可以想到两种方法来解决这个问题:

  1. 既然你在 Ubuntu,试试 运行ning sudo apt-get install python3-scapy 看看它现在是否可以 运行 from scapy.all import sniff
  2. 检查 python 当前从中提取的路径,方法是在终端中键入以下内容:
$ which scapy
  /location-of-scapy-on-your-system

然后运行

$ python
  >>> import os
  >>> scapy_path = "/location-of-scapy-on-your-system"
  >>> if not scapy_path in os.sys.path: os.sys.path.append(scapy_path)

这应该有望解决问题。如果没有,可能是你安装了错误的scapy版本:你可以通过运行ning

来检查
$ python
  >>> import scapy
  >>> print(scapy.__version__)