ImportError: cannot import name 'http' from 'scapy.layers - scapy

I am getting this error and I don't understand why?
ImportError: cannot import name 'http' from 'scapy.layers
Here is my code:
import scapy.all as scapy
from scapy.layers import http #error line
def sniff(interface):
scapy.sniff(iface=interface,store=False,prn=p_s_p)
def p_s_p(packet):
if packet.haslayer(http.HTTPRequest):
print(packet)
sniff('wlan0')

To make things clear:
scapy-http is deprecated starting from Scapy v2.4.3+.
Scapy 2.4.3+ now includes a modified (improved) version of scapy-http that is disabled by default, to be backward compatible. You can load it using:
from scapy.layers.http import *
load_layer("http") (if in the console)

from scapy.layers.http import *
Finer points of scapy importing in python 3* are discussed in the following answer -- Cannot get scapy 2.4.3 http layer support to work
Weirdly absent from the documentation.

pip3 install scapy==2.4.5
It should fix your problem

Use the scapy_http library:
from scapy_http import http

install the scapy http library from the terminal:
pip install scapy-http

Related

In robot framework, python library urllib3 module doesn't have attribute pathname2url

I tried to upgrade a few packages with pip and also my python from 3.9 to 3.9.13. so now i use RIDE v2.0b1 running on Python 3.9.13. i use robotframework-sudslibrary-aljcalandra 1.1.4. instead of 1.1 and urllib3 1.26.12 instead of 1.26.9 for calls to webservices
I get an error when running a keyword of sudslibrary (Create Soap Client)
FAIL : AttributeError: module 'urllib' has no attribute 'pathname2url'
Since i don't have any knowledge of python and all solutions on google are about python, i'm stuck with this error. could anybody please help me?
In the following site
https://itecnote.com/tecnote/python-3-4-2-urlib-no-attribute-pathname2url/ it says that pathname2url function of urllib is now in another library which is urllib.request.pathname2url.
Should i install this python library independently by pip?
it's strange because with my older installations (ython 3.9) i didn't have urllib.request.pathname2url library when i pip(ed) list it.
Thank you for your precious help i'm badly stucked
This seems a problem on the sudslibrary you are using. A possible workaround is to fix the offending code by replacing with the new module reference.
You could (should?) find all references to the import or use of pathname2urlin that module library and replace with fixing code. For example:
import urllib.request.pathname2url as pathname2url
or
if you have from urllib import pathname2url
from urllib.request import pathname2url
The best would be, after identifying the fix, to propose a Pull Request for the sudslibrary, with something like:
try:
from urllib import pathname2url
except ImportError:
from urllib.request import pathname2url
And similar for the absolut imports.

How can I install parse for python3 if I get importError?

So I'm working in Linux and I need to install parse for python3 but always get the same error: ImportError: No module named parse. I tried it:
from urllib.parse import urlparse
from parser import *
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse (but as I know its only for python2, I work on python3).
Also tried to do this pip install parse but had no result. Before it I had the next error “NameError: global name 'parse' is not defined”.
Please can you help me, what should I do? I found that some people have the same problem but their resolutions dont help me
urllib is in standard library, no need to install. It works ok for me in python 3.x. Probably you have named your script(the .py file you are running) to urllib. This is a common mistake, rename it to something else then it works.
It could happen even if you have a python file named urllib in your directory... because when you run your script, python will automatically add it's directory to sys.path(where python searched for modules/packages). So it gets reached sooner than the original urllib which is in the standard library.
Search that file in your directory and delete it.

Scapy not working with Python 3.6. Import problems with Python?

I am trying to test a simple code that will inspect a .pcap file using scapy. My OS is Ubuntu 18.04 and my Python version is 3.6.
However when I do: from scapy.all import * I get the error message ModuleNotFoundError: No module named 'scapy.all'.
When I change this to be from scapy import *, I get an error later in my code when I try and use the scapy sendrecv sniff function. I get the error NameError: name 'sniff' is not defined. Note if I switch from sniff() to rdpcap() I get the same error but its now "rdpcap is not defined".
I've read through a bunch of previous stack overflow answers and nothing is helping. My python script name is "pcap_grapher.py", so the issue is not the script name being scapy.py.
When I run pip3 install scapy in the terminal I get the message Requirement already satisfied: scapy in /home/vic/.local/lib/python3.6/site-packages (2.4.4)
I've also tried running pip3 install --pre scapy[basic] as the docs recommend here: https://scapy.readthedocs.io/en/latest/installation.html but it didn't help.
Any advice would be greatly appreciated. I am super stuck.
First off, you will never be able to import sniff() from scapy, since it is within the submodule scapy.all. There are two ways I can think of to fix this:
Since you're on Ubuntu, try running sudo apt-get install python3-scapy and see if it lets you run from scapy.all import sniff now.
Check the path that python is currently pulling from by typing the following in terminal:
$ which scapy
/location-of-scapy-on-your-system
Then run
$ python
>>> import os
>>> scapy_path = "/location-of-scapy-on-your-system"
>>> if not scapy_path in os.sys.path: os.sys.path.append(scapy_path)
That should hopefully fix things. If not, it's possible you installed the wrong version of scapy: you can check this by running
$ python
>>> import scapy
>>> print(scapy.__version__)

ImportError with dask.distributed

I am trying to import dask.distributed package, but I keep getting this error: ImportError: cannot import name 'collections_to_dsk'. Help is appreciated.
I resolved the error, there were some outdated packages including imageio which needed upgrade to work with dask.distributed and dask.dataframe.

issue plotting .cap files ScaPy Python3

After reading .cap file using rdpcap successfully, I've been trying to use the function pdfdump but it keeps giving me an error:
"AttributeError: 'SingleTexRunner' object has no attribute 'texoutput' "
here's my code:
from scapy.all import *
import pyx
a = rdpcap("the file path..")
a.pdfdump("output filename")
anyone got an idea how to solve this issue?
Use sudo apt-get install python3-pyx on Linux(Debian, Ubuntu...) to install pyx and its dependencies; installing pyx with pip install pyx is not enough.
I'm using Python 3.6.0
I've installed scapy with:
pip3 install scapy-python3
Using wireshark 2.2.3, I saved a dump using the default file format which is .pcapng
I've used the same code as you :
from scapy.all import *
import pyx
a = rdpcap("test.pcapng")
a.pdfdump("test.pdf")
and I obtain a valid pdf.
Don't run scapy (if you use the command line) or your script as root. This fixed it for me.

Resources