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

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')


Solution 1:[1]

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)

Solution 2:[2]

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.

Solution 3:[3]

pip3 install scapy==2.4.5

It should fix your problem

Solution 4:[4]

Use the scapy_http library:

from scapy_http import http

Solution 5:[5]

install the scapy http library from the terminal:

pip install scapy-http

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cukic0d
Solution 2 Nate Taylor
Solution 3 Newt
Solution 4 jfleach
Solution 5 sofblocks