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

Adding an extra header in HTTP packets


Sometimes, you would like to manipulate an application by supplying a custom HTTP header that contains custom information. For example, adding an authorization header can be useful to implement the HTTP basic authentication in your packet capture code. As with the previous recipe, this recipe requires admin privileges to run too.

How to do it...

Let us sniff the packets using the sniff() function of Scapy and define a callback function, modify_packet_header(), which adds an extra header of certain packets.

Listing 8.3 gives the code for adding an extra header in HTTP packets, 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. 
 
 
from scapy.all import * 
 
def modify_packet_header(pkt): 
    """ Parse the header and add an extra header""" 
    if pkt.haslayer(TCP) and pkt.getlayer...
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 AU $24.99/month. Cancel anytime