Book Image

Python Network Programming

By : Abhishek Ratan, Eric Chou, Pradeeban Kathiravelu, Dr. M. O. Faruque Sarker
Book Image

Python Network Programming

By: Abhishek Ratan, Eric Chou, Pradeeban Kathiravelu, Dr. M. O. Faruque Sarker

Overview of this book

This Learning Path highlights major aspects of Python network programming such as writing simple networking clients, creating and deploying SDN and NFV systems, and extending your network with Mininet. You’ll also learn how to automate legacy and the latest network devices. As you progress through the chapters, you’ll use Python for DevOps and open source tools to test, secure, and analyze your network. Toward the end, you'll develop client-side applications, such as web API clients, email clients, SSH, and FTP, using socket programming. By the end of this Learning Path, you will have learned how to analyze a network's security vulnerabilities using advanced network packet capture and analysis techniques. This Learning Path includes content from the following Packt products: • Practical Network Automation by Abhishek Ratan • Mastering Python Networking by Eric Chou • Python Network Programming Cookbook, Second Edition by Pradeeban Kathiravelu, Dr. M. O. Faruque Sarker
Table of Contents (30 chapters)
Title Page
Copyright
About Packt
Contributors
Preface
Index

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 reuse 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 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...