Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Network Programming

You're reading from   Python Network Programming Conquer all your networking challenges with the powerful Python language

Arrow left icon
Product type Course
Published in Jan 2019
Publisher Packt
ISBN-13 9781788835466
Length 776 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Authors (4):
Arrow left icon
Dr. M. O. Faruque Sarker Dr. M. O. Faruque Sarker
Author Profile Icon Dr. M. O. Faruque Sarker
Dr. M. O. Faruque Sarker
Eric Chou Eric Chou
Author Profile Icon Eric Chou
Eric Chou
Abhishek Ratan Abhishek Ratan
Author Profile Icon Abhishek Ratan
Abhishek Ratan
Pradeeban Kathiravelu Pradeeban Kathiravelu
Author Profile Icon Pradeeban Kathiravelu
Pradeeban Kathiravelu
Arrow right icon
View More author details
Toc

Table of Contents (30) Chapters Close

Title Page
Copyright
About Packt
Contributors
Preface
1. Fundamental Concepts FREE CHAPTER 2. Python for Network Engineers 3. Continuous Integration for Network Engineers 4. SDN Concepts in Network Automation 5. Low-Level Network Device Interactions 6. APIs and Intent-Driven Networking 7. The Python Automation Framework – Ansible Basics 8. The Python Automation Framework – Beyond Basics 9. AWS Cloud Networking 10. Working with Git 11. Sockets, IPv4, and Simple Client/Server Programming 12. Multiplexing Socket I/O for Better Performance 13. IPv6, Unix Domain Sockets, and Network Interfaces 14. Programming with HTTP for the Internet 15. Email Protocols, FTP, and CGI Programming 16. Programming Across Machine Boundaries 17. Working with Web Services – SOAP, and REST 18. Network Monitoring and Security 19. Network Modeling 20. Authentication, Authorization, and Accounting (AAA) 21. Open and Proprietary Networking Solutions 22. NFV and Orchestration – A Larger Ecosystem 23. Programming the Internet 1. Other Books You May Enjoy Index

Customizing the IP address of a packet


If you ever need to create a network packet and customize the source and destination IP or ports, this recipe can serve as the starting point.

How to do it...

We can take all the useful command-line arguments such as network interface name, protocol name, source IP, source port, destination IP, destination port, and optional TCP flags.

We can use the Scapy library to create a custom TCP or UDP packet and send it over the network. As with the previous recipes, this recipe requires admin privilege to run.

Listing 8.5 gives the code for customizing the IP address of a packet, 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 other version with/without modifications. 
 
 
import argparse 
import sys 
import re 
from random import randint 
 
from scapy.all import IP,TCP,UDP,conf,send 
 
 
def send_packet(protocol=None, src_ip...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime