简体   繁体   English

PyCharm:与 Scapy 的未解决参考

[英]PyCharm: Unresolved reference with Scapy

I am working on a network tool that I write in python using scapy.我正在使用 scapy 在 python 中编写一个网络工具。
As IDE I am using Pycharm.作为 IDE,我使用 Pycharm。
My Code works.我的代码有效。 So if I run it, everything works just as intended.所以如果我运行它,一切都按预期工作。

My problem is that PyCharm is giving me some errors.我的问题是 PyCharm 给了我一些错误。
It marks every use of IP , TCP , Ether , ... as Undefined Reference to ...它将IPTCPEther每次使用都标记为Undefined Reference to ...

The relevant parts of my Code look like this我的代码的相关部分如下所示

#!/usr/bin/env python
from scapy.all import *

...  
...  

syn = IP(src=src_ip, dst=dst_ip) / TCP(sport=src_port, dport=dst_port, seq=src_seq, flags="S")

...

I tried many things I found using google, like adding my src folder as source root, I refreshed all caches I could find and restarted PyCharm dozens of times, but nothing worked...我尝试了很多我使用谷歌发现的东西,比如将我的 src 文件夹添加为源根目录,我刷新了我能找到的所有缓存并重新启动了 PyCharm 数十次,但没有任何效果......

Since the code works it's a minor problem, but still I'd like to have my IDE working as intended由于代码有效,这是一个小问题,但我仍然希望我的 IDE 按预期工作

I am working under MacOS and I use a Virtual Environment我在 MacOS 下工作,我使用虚拟环境

This is a PyCharm issue.这是一个 PyCharm 问题。 Scapy usesdynamic loading (using importlib ) to load many modules / custom modules, that pycharm does not detect. Scapy 使用动态加载(使用importlib )来加载许多 pycharm 未检测到的模块/自定义模块。 This allows the users to select which layers they want to have loaded.这允许用户选择他们想要加载的图层。

The workaround is to import whatever you need from their related scapy file, without using all .解决方法是从相关的 scapy 文件中导入您需要的任何内容,而不使用all . It is cleaner but longer to do.它更干净,但要做的时间更长。 Or you can use "add an exception" in your IDE, if you're not looking for something clean.或者,如果您不是在寻找干净的东西,您可以在 IDE 中使用“添加异常”。

Here are a few useful modules这里有一些有用的模块

  • scapy.layers.inet where you can get IP, TCP.. scapy.layers.inet在那里你可以得到 IP、TCP ..
  • scapy.layers.inet6
  • scapy.layers.dns
  • scapy.sendrecv has srp, sr, sr1, sendp, send... scapy.sendrecv有 srp、sr、sr1、sendp、send...
  • scapy.supersocket to directly access scapy's sockets scapy.supersocket直接访问 scapy 的套接字
  • scapy.layers.l2 which has Ether, ARP.. scapy.layers.l2有 Ether、ARP ..
  • scapy.layers.dot11 for 802.11 stuff scapy.layers.dot11用于 802.11 的东西
  • scapy.utils for wrpcap , rdpcap ... scapy.utils用于wrpcap , rdpcap ...
  • scapy.config for the conf object (which has properties such as conf.route or conf.route6 ) scapy.config用于conf对象(具有conf.routeconf.route6等属性)

What I advise to do is to open the Scapy shell (or import from scapy.all import * in a console) and check from which module a layer/function is by using help(...) .我建议做的是打开 Scapy shell(或在控制台中from scapy.all import * )并使用help(...)检查层/函数来自哪个模块。 You can also check out the online API reference (it has a search bar) over here您还可以在此处查看在线 API 参考(它有一个搜索栏)

Had the same issue, try importing this way:有同样的问题,请尝试以这种方式导入:

from scapy.layers.inet import IP, UDP, wrpcap, Ether

it worked for me.它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM