Python library issues

I'm relatively new to Python, and I'm having trouble programming with Scapy, a Python network manipulation toolkit. However, I cannot say if this is a Scapy problem, since it is a problem with the newbie-creature-Python-newbie. On the scapy site, they give an example of a program that I cannot run on my machine:

#! /usr/bin/env python import sys from scapy import sr1,IP,ICMP p=sr1(IP(dst=sys.argv[1])/ICMP()) if p: p.show() 

What will I get:

 Traceback (most recent call last): File "test.py", line 4, in <module> from scapy import sr1,IP,ICMP ImportError: cannot import name sr1 

So my question is: do I need to change my path or something similar when installing Python libraries? Also, is there something I can run in the interpreter to tell me the contents of the scapy package? I can run from scapy import * just fine, but since I have no idea what's inside it, it's hard to use it.

+6
python networking scapy
source share
4 answers

With a caution from Federico Ramponi β€œYou must use scapy as an interpreter yourself, not as a library,” I want to answer the non-scapy-specific parts of the question.

Q: when installing Python libraries, do I need to change my path or something similar?

A: I think you are talking about changing PYTHONPATH on a system scale. Usually this is not required or a good idea.

Third-party Python libraries must either be installed in system directories, for example, /usr/lib/python2.5/site-packages , or installed locally, in which case you may need to install PYTHONPATH in the Makefile or in the shell of the driver script.

Q: Also, is there something I can run in the interpreter to tell me the contents of the scapy package?

A: You can do something like this:

 >>> import scapy >>> dir(scapy) 

Or even better:

 >>> import scapy >>> help(scapy) 

Bonus question asked in comments.

Q: Is "import scapy" the same as "from scapy import"?

A: import scapy associates the scapy name in the local namespace with the scapy module object. OTOH, from scapy import * does not bind the module name, but all public names defined in the scapy module are connected in the local namespace.

See paragraphs 6 and 7 of the Python Reference, 6.12 Import operation .

+6
source share

I had the same problem in scapy v2.x use

  from scapy.all import * 

instead of v1.x

  from scapy import * 

as written here

Enjoy it =)

+4
source share

He tells you that he cannot find sr1 in scapy. Not sure how new you are, but the translator is always your friend. Run the interpreter (just type "python" at the command line), and at the prompt (β†’>) enter (but do not type ">", they will appear themselves):

 >>> import scapy >>> from pprint import pformat >>> pformat(dir(scapy)) 

The last line should print a lot of material. Do you see sr1, IP and ICMP there? If not, the example is faulty.

Try also help (scapy)

What about how I can help you without installing scapy and looking at your source file myself.

+3
source share

The scapy package is a tool for network management and monitoring. I'm curious what you are trying to do with this. It's rude to spy on friends. :-)

 coventry@metta :~/src$ wget -q http://www.secdev.org/projects/scapy/files/scapy-latest.zip coventry@metta :~/src$ unzip -qq scapy-latest.zip warning [scapy-latest.zip]: 61 extra bytes at beginning or within zipfile (attempting to process anyway) coventry@metta :~/src$ find scapy-2.0.0.10 -name \*.py | xargs grep sr1 scapy-2.0.0.10/scapy/layers/dns.py: r=sr1(IP(dst=nameserver)/UDP()/DNS(opcode=5, scapy-2.0.0.10/scapy/layers/dns.py: r=sr1(IP(dst=nameserver)/UDP()/DNS(opcode=5, scapy-2.0.0.10/scapy/layers/inet6.py:from scapy.sendrecv import sr,sr1,srp1 scapy-2.0.0.10/scapy/layers/snmp.py: r = sr1(IP(dst=dst)/UDP(sport=RandShort())/SNMP(community=community, PDU=SNMPnext(varbindlist=[SNMPvarbind(oid=oid)])),timeout=2, chainCC=1, verbose=0, retry=2) scapy-2.0.0.10/scapy/layers/inet.py:from scapy.sendrecv import sr,sr1,srp1 scapy-2.0.0.10/scapy/layers/inet.py: p = sr1(IP(dst=target, options="\x00"*40, proto=200)/"XXXXYYYYYYYYYYYY",timeout=timeout,verbose=0) scapy-2.0.0.10/scapy/sendrecv.py:def sr1(x,filter=None,iface=None, nofilter=0, *args,**kargs): 

According to the last line, sr1 is a function defined in scapy.sendrecv . Someone should write a mistake with the author.

+1
source share

All Articles