Scapy.all import * does not work

So, I wrote a little script in Ubuntu for scapy.

#!/usr/bin/env python
import sys
#from scapy.all import *
try 
   import scapy
   except ImportError:
     del scapy
     from scapy import all as scapy
i= IP()
t= TCP()
i.dst='192.168.56.100'
t.dport=22
pakket=i/t
answered,unanswered=sr(pakket)
answered.nsummary()

i wrote the 'try' because of another topic here (tried it as a solution). My current output with this code is the following

Traceback (most recent call last):
File "./scapy.py", line 5, in <module>
import scapy
File "/home/nicholas/scapy.py", line 9, in <module>
i=IP()
NameError: name 'IP' is not defined

when trying it just with from scapy.all import * withouth the 'try'.

Traceback (most recent call last):
File "./scapy.py", line 3, in <module>
from scapy.all import *
File "/home/nicholas/scapy.py", line 3, in <module>
from scapy.all import *
ImportError: No module named all

I tried different ways of importation found on Google but it still doesn't work. Could anyone please tell me what I'm doing wrong? (don't mind the indentation of this post)


From looking at scapy source, the scapy package doesn't appear to import anything or define an __all__ in __init__ . As a result, you need to explicitly import scapy.all (or from scapy import all ) before you can from scapy.all import anything else from it, as it won't be in sys.modules yet. Note that this only has to happen once in your program flow though, as after the interpreter imports the module, it will be available to all code that executes from then on, regardless of where it is. Take a look at the Python docs on modules and how import , and specifically importing a package, works for more details.

Edit: I think I see the problem now, I just was paying attention to the wrong part of your stack trace. Pretty sure what you are dealing with here is a name collision. Your file is named scapy.py , so when you import scapy from the context of that file, you are actually importing the file itself as a module. Since your file does not have a submodule named all (it can't, since it's not a package), you get the import error you are seeing. Try switching the name of your file to something that does not conflict with any packages or modules you wish to import inside it, and see if that works out better.

By the way, note in your stack traces that your import is actually essentially recursively calling your one file. That should be a clue that something has gone haywire in the import process.


Quite old post. For the ones still looking, the correct import with current versions would be:

from scapy.all import *


I saw this when I had a scapy.py in the current directory. scapy.all import * seems to look in the current directory first.

链接地址: http://www.djcxy.com/p/11524.html

上一篇: 如何在应用程序的活动状态下将远程推送通知显示为横幅样式?

下一篇: Scapy.all导入*不起作用