O'Reilly logo

Python Network Programming Cookbook - Second Edition by Dr. M. O. Faruque Sarker, Pradeeban Kathiravelu

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

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 is not recommended for production code.

The sniff() function of Scapy takes the name of a callback function. Let's write a callback function that will write the packets onto a file.

Listing 8.2 gives the code for saving packets in the pcap format using the pcap dumper, as follows:

 #!/usr/bin/env python # Python Network Programming Cookbook, Second Edition -- Chapter - 8 # This program is optimized for Python 2.7.12 and Python 3.5.2. # It may run on any ...

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