Search code examples
pythonpcapscapysniffing

Generate a pcap file at the end of a Python script


I wrote a Python script, using Scapy tool, to generate and send packets from a client to server after establishing a TCP connection. I want to generate a pcap file, at the end of my script. The pcap file should contain all traffic generated by the current script. I tried with sniff() but the problem is that sniff(), when executed, pauses the script and it couldn't run at the same time with other instructions. How can I do this in Scapy?


Solution

  • To write a list of packets to a PCAP use the wrpcap function.

    >>> wrpcap("temp.cap", pkts)
    

    pkts is a list of packets. For your implementation you will need to maintain a list of all packets that have been sent and received as your program runs. You can't run a capture and send packets in Scapy.