O'Reilly logo

Python Network Programming Cookbook by Dr. M. O. Faruque Sarker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Saving packets in the pcap format using the pcap dumper

The pcap format, abbreviated from packet capture, is a common file format for saving network data. More details on the pcap format can be found at http://wiki.wireshark.org/Development/LibpcapFileFormat.

If you want to save your captured network packets to a file and later re-use them for further processing, this recipe can be a working example for you.

How to do it...

In this recipe, we use the Scapy library to sniff packets and write to a file. All utility functions and definitions of Scapy can be imported using the wild card import, as shown in the following command:

from scapy.all import *

This is only for demonstration purposes and not recommended for production code.

The sniff() function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required