问题
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
回答1:
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.inetwhere you can get IP, TCP..scapy.layers.inet6scapy.layers.dnsscapy.sendrecvhas srp, sr, sr1, sendp, send...scapy.supersocketto directly access scapy’s socketsscapy.layers.l2which has Ether, ARP..scapy.layers.dot11for 802.11 stuffscapy.utilsforwrpcap,rdpcap...scapy.configfor theconfobject (which has properties such asconf.routeorconf.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(...)
回答2:
Had the same issue, try importing this way:
from scapy.layers.inet import IP, UDP, wrpcap, Ether
it worked for me.
来源:https://stackoverflow.com/questions/45691654/unresolved-reference-with-scapy