pythonjsonpacket-sniffers

Send Scapy serialized packets via JSON (api call)


I am struggling with serializing packets from scapy so I can send as JSON objects via an api call.

Right now, my api call looks like so:

def load_points(packet_list, permanent):
    print("loading points")

    waypoint_url = ''
    if(permanent):
        waypoint_url = "/add_permanent_points_packets"
    else:
        waypoint_url = "/add_temporary_points_packets"
    
    response = requests.post(
        URL+waypoint_url,
        json={        
        "lidar_params": {
                "azimuth_degrees": 0.5,
                "elevation_degrees": 5,
                "radial_resolution": 2,
                "max_elevation": 15,
                "min_elevation": -15,
                "max_radius": 100,
                "floor_height": beacon_height,
                "latitude": beacon_latitude,
                "longitude": beacon_longitude
            },
        "packets": json.dumps(packet_list)
        },
    )
    print(response.text)

The main point of concern above is the packets field. I am trying to pass a list of packets and I am not entirely sure how to serialize it to make it a valid JSON object. THE API SERVER IS EXPECTING A LIST OF STRINGS. So the main questions is How do you convert a list of packets to a list of strings to send via JSON.

Below is how I get the list of packets:

sniff(prn=permanent_packet_callback, iface="Ethernet 5",count=revolution_count)

def permanent_packet_callback(packet):
    global packet_list, done_waiting

    # append to list
    packet_list.append(packet)

Solution

  • Simply append the packet to the packet list as follows:

    packet_list.append(f"{packet}")