Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

python - PyCharm: Unresolved reference with Scapy

I am working on a network tool that I write in python using scapy.
As IDE I am using Pycharm.
My Code works. So if I run it, everything works just as intended.

My problem is that PyCharm is giving me some errors.
It marks every use of IP, TCP, Ether, ... as 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...

Since the code works it's a minor problem, but still I'd like to have my IDE working as intended

I am working under MacOS and I use a Virtual Environment

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a PyCharm issue. Scapy uses dynamic loading (using importlib) to load many modules / custom modules, that pycharm does not detect. 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. 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.

Here are a few useful modules

  • scapy.layers.inet where you can get IP, TCP..
  • scapy.layers.inet6
  • scapy.layers.dns
  • scapy.sendrecv has srp, sr, sr1, sendp, send...
  • scapy.supersocket to directly access scapy’s sockets
  • scapy.layers.l2 which has Ether, ARP..
  • scapy.layers.dot11 for 802.11 stuff
  • scapy.utils for wrpcap, rdpcap...
  • scapy.config for the conf object (which has properties such as conf.route or conf.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(...). You can also check out the online API reference (it has a search bar) over here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...