Python Network Programming Cookbook, Second Edition 2017 is his first book as an author, and he is quite excited about it... to work on the Google Summer of Code 2009/2010 programs forco
Trang 3Python Network Programming Cookbook
Trang 4Second Edition
Overcome real-world networking challenges
Trang 5Pradeeban Kathiravelu
Dr M O Faruque Sarker
Trang 6BIRMINGHAM - MUMBAI
Trang 8Python Network Programming Cookbook
Trang 9Second Edition
Copyright © 2017 Packt Publishing
All rights reserved No part of this book may be reproduced, stored
in a retrieval system, or transmitted in any form or by any means,without the prior written permission of the publisher, except in thecase of brief quotations embedded in critical articles or reviews
Every effort has been made in the preparation of this book to
ensure the accuracy of the information presented However, theinformation contained in this book is sold without warranty, eitherexpress or implied Neither the authors, nor Packt Publishing, andits dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book
Packt Publishing has endeavored to provide trademark informationabout all of the companies and products mentioned in this book bythe appropriate use of capitals However, Packt Publishing cannotguarantee the accuracy of this information
First published: March 2014
Second edition: August 2017
Trang 14About the Author
Pradeeban Kathiravelu is an open source evangelist He is a
Ph.D researcher at INESC-ID Lisboa/Instituto Superior Tecnico,Universidade de Lisboa, Portugal, and Universite Catholique deLouvain, Belgium He is a Fellow of Erasmus Mundus Joint
Degree in Distributed Computing (EMJD-DC), researching a
software-defined approach to quality of service and data quality inmulti-tenant clouds
Pradeeban holds a master of science degree, Erasmus Mundus
European Master in Distributed Computing (EMDC), from
Instituto Superior Tecnico, Portugal and KTH Royal Institute ofTechnology, Sweden He also holds a first class bachelor of science
in engineering (Hons) degree, majoring in computer science andengineering, from the University of Moratuwa, Sri Lanka His
research interests include Software-Defined Networking (SDN),distributed systems, cloud computing, web services, big data inbiomedical informatics, Network Functions Virtualizations (NFV),and data mining He is very interested in free and open source
software development and has been an active participant in the
Google Summer of Code (GSoC) program since 2009, as a studentand as a mentor
Pradeeban has published several conference papers and
co-authored a few book chapters He has also worked on
OpenDaylight Cookbook and Learning OpenDaylight as a technical
reviewer Python Network Programming Cookbook, Second
Edition (2017) is his first book as an author, and he is quite excited
about it
Trang 15I would like to thank my readers for the interest in the book Please feel free to contact me if you need any assistance in the topics or the recipes, beyond what we have discussed in the book I would like to thank the entire editorial team at Packt, including Abhishek Jadhav, Rahul Nair, and Mohd Riyan Khan I would like to extend
my thanks to the Linux Foundation for their open source projects
on softwarization of networks and systems I would like to thank my friends and colleagues who helped me in various ways I would like
to thank Prof Luis Veiga (INESC-ID Lisboa), my MSc and Ph.D advisor, for sharing his wisdom and encouragement throughout my stay in Instituto Superior Tecnico I would like to thank him for being my mentor since 2012 I would also like to thank Prof Ashish Sharma (Emory University, Atlanta) for his guidance and
motivation.
My special thanks go to my loving wife, Juejing Gu This book
would not be a reality without her continuous support and creative suggestions Her tireless efforts helped me always be on time
without missing the deadlines.
I would like to thank my mom, Selvathie Kathiravelu, for her
support.
Dr M O Faruque Sarker is a software architect based in
London, UK, where he has been shaping various Linux and opensource software solutions, mainly on cloud computing platforms,for commercial companies, educational institutions, and
multinational consultancies Over the past 10 years, he has beenleading a number of Python software development and cloud
infrastructure automation projects In 2009, he started using
Python, where he was responsible for shepherding a fleet of
miniature E-puck robots at the University of South Wales,
Newport, UK Later, he honed his Python skills, and he was invited
Trang 16to work on the Google Summer of Code (2009/2010) programs forcontributing to the BlueZ and Tahoe-LAFS open source projects.
He is the author of Python Network Programming Cookbook and
Learning Python Network Programming both by Packt Publishing.
He received his Ph.D in multi-robot systems from the University
of South Wales He is currently working at University CollegeLondon He takes an active interest in cloud computing, softwaresecurity, intelligent systems, and child-centric education He lives
in East London with his wife, Shahinur, and daughter, Ayesha
All praises and thanks to Allah, the God who is the Merciful and the Beneficent I would not be able to finish this book without the help of God.I would like to thank everyone who has contributed to the publication of this book, including the publisher, technical
reviewers, editors, my family and friends for their sacrifice of time, encouraging words, and smiles, especially my wife Shahinur
Rijuani for her love and support in my work I also thank the
readers who have patiently been waiting for this book and who have given me lots of valuable feedback.
Trang 17About the Reviewers
Dr S Gowrishankar is currently working as an associate
professor in the Department of Computer Science and Engineering
at Dr Ambedkar Institute of Technology, Bengaluru, Karnataka,India
He received his Ph.D in Engineering from Jadavpur University,Kolkata, West Bengal, India in 2010, MTech in softwareengineering and BE in computer science and engineering fromVisvesvaraya Technological University (VTU), Belagavi,Karnataka, India in the year 2005 and 2003 respectively
From 2011 to 2014 he worked as a senior research scientist andtech lead at Honeywell Technology Solutions, Bengaluru,Karnataka, India
He has published several papers in various reputed internationaljournals and conferences He is serving as an editor and reviewerfor various prestigious international journals He is also a member
of IEEE, ACM, CSI, and ISTE
He has delivered many keynote addresses and invited talksthroughout India on a variety of subjects related to computerscience and engineering He was instrumental in organizing severalconferences, workshops, and seminars He has also served on thepanel of a number of academic bodies of universities andautonomous colleges as a BOS and BOE member
His current research interests are mainly focused on data science,including its technical aspects as well as its applications and
Trang 18implications Specifically, he is interested in the applications ofMachine Learning, Data Mining, and Big Data Analytics inHealthcare.
I would like to acknowledge my earnest gratitude to my wife,
Roopa K M, for her constant source of support and encouragement throughout this assignment I'm truly thankful to almighty God for having her in my life and give her my deepest expression of love and appreciation.
Michael Bright, RHCE/RHCSA, is a solution architect working in
the HPE EMEA Customer Innovation Center
He has strong experience across Cloud and Container technologies(Docker, Kubernetes, AWS, GCP, Azure) as well as NFV/SDN
Based in Grenoble, France, he runs a Python user group and is aco-organizer of the Docker and FOSS Meetup groups He has akeen interest in Container, Orchestration, and Unikernel
technologies on which he has presented and run training tutorials inseveral conferences
He has presented many times on subjects diverse as NFV, Docker,Container Orchestration, Unikernels, Jupyter Notebooks,
MongoDB, and Tmux
Michael has a wealth of experience across pure research, R&D andpre-sales consulting roles
Trang 19For support files and downloads related to your book, please visit w ww.PacktPub.com
Did you know that Packt offers eBook versions of every book
published, with PDF and ePub files available? You can upgrade tothe eBook version at www.PacktPub.comand as a print book
customer, you are entitled to a discount on the eBook copy Get intouch with us at service@packtpub.com for more details
At www.PacktPub.com, you can also read a collection of free
technical articles, sign up for a range of free newsletters and
receive exclusive discounts and offers on Packt books and eBooks
Trang 21If you'd like to join our team of regular reviewers, you can e-mail
us at customerreviews@packtpub.com We award our regular
reviewers with free eBooks and videos in exchange for their
valuable feedback Help us be relentless in improving our products!
Trang 22I dedicate this book to the world, in memory of my dad,
Kanapathipillai Kathiravelu.
Trang 23Table of Contents
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready How to do it…
How it works…
There's more…
See also Conventions
Reader feedback
Customer support
Downloading the example code Downloading the color images of this book Errata
Piracy Questions
1 Sockets, IPv4, and Simple Client/Server Programming
Trang 25Writing a simple UDP echo client/server application
How it works
Waiting for a remote network service
How to do it
Trang 27How it works
Sending web requests through a proxy server
Getting ready How to do it
Trang 28Writing a simple HTTPS server code with Python and OpenSSL Getting ready
How to do it
How it works
Building asynchronous network applications with Twisted
Getting ready How to do it
How it works
Building asynchronous network applications with Tornado
Getting ready How to do it
How it works
Building concurrent applications with Tornado Future
Getting ready How to do it
How it works
Common error Uploading a local file to a remote FTP server
Getting ready How to do it
Trang 29Getting ready How to do it
How it works
Checking your remote email with IMAP
Getting ready How to do it
How it works
Writing a simple SMTP server
Getting ready How to do it
How it works
Writing a secure SMTP client using TLS
Getting ready How to do it
How it works
Writing an email client with POP3
Getting ready How to do it
How it works
6 Programming Across Machine Boundaries
Trang 30Executing a remote shell command using telnet
Getting ready How to do it
How it works
Copying a file to a remote machine by SFTP
Getting ready How to do it
How it works
Printing a remote machine's CPU information
Getting ready How to do it
How it works
Installing a Python package remotely
Getting ready How to do it
How it works
Running a MySQL command remotely
Getting ready How to do it
How it works
Transferring files to a remote machine over SSH
Getting ready How to do it
How it works
Configuring Apache remotely to host a website
Getting ready How to do it
Trang 31Getting ready How to do it
How it works
Saving packets in the pcap format using the pcap dumper
How to do it
Trang 32How it works
Emulating networks with Mininet
Getting ready How to do it
How it works
Distributed network emulation with MaxiNet
Getting ready How to do it
How it works
Emulating wireless networks with Mininet-WiFi Getting ready
How to do it
Trang 33How it works
Extending Mininet to emulate containers
Getting ready How to do it
How it works
Developing Software-Defined Networks with OpenDaylight contro ller
Getting ready How to do it
How it works
Developing Software-Defined Networks with POX controller
Getting ready How to do it
How it works
Developing Software-Defined Networks visually with MiniEdit
Trang 34Getting ready How to do it
How it works
Finding DNS host information
Getting ready How to do it
How it works
Finding DNS resource records
Getting ready How to do it
How it works
Making DNS zone transfer
Getting ready How to do it
How it works
Querying NTP servers
Getting ready How to do it
How it works
Connecting to an LDAP server
Getting ready How to do it
How it works
Making LDAP bind
Getting ready How to do it
Trang 35How it works
Reading and writing LDAP
Getting ready How to do it
How it works
Authenticating REST APIs with Eve
Getting ready How to do it
How it works
Configuring OpenContrail cluster
Trang 36How it works
Packet processing with DPDK
Getting ready How to do it
How it works
Parsing BMP messages with SNAS.io
Getting ready How to do it
How it works
Trang 3714 Programming the Internet
Introduction
Checking a website status
Getting ready How to do it
Looking glass implementations with Python
Getting ready How to do it
How it works
Trang 38It has been more than 3 years since Python Network Programming
Cookbook was first published In this second edition, we extend our
book to discuss the recent advancements in the networking industryand network softwarization The widespread use of Software-
Defined Networking (SDN), Network Functions Virtualization(NFV), and orchestration have been addressed in detail in the latterchapters while the first eight chapters were taken from the first
edition, improved with a few new recipes based on the feedbackfrom the readers
This book is an exploratory guide to network programming in
Python It has touched a wide range of networking protocols such
as TCP/UDP, HTTP/HTTPS, FTP, SMTP, POP3, IMAP, and CGI.With the power and interactivity of Python, it brings joy and fun todevelop various scripts for performing real-world tasks on networkand system administration, web application development,
interacting with your local and remote network, low-level networkpacket capture and analysis, and so on The primary focus of thisbook is to give you a hands-on experience on the topics covered
So, this book covers less theory, but it is packed with practical
materials
This book is written with a DevOps mindset, where a developer isalso more or less in charge of operation, that is, deploying the
application and managing various aspects of it, such as remote
server administration, monitoring, scaling-up, and optimizing forbetter performance This book introduces you to a bunch of open-source, third-party Python libraries, which are ideal to be used in
Trang 39various use cases We elaborate in detail the configurations ofcomplex networking systems with helpful hints to ensure that thereader can follow them without getting stuck.
We hope you will enjoy the recipes presented in this book andextend them to make them even more powerful and enjoyable
Trang 40What this book covers
Chapter 1, Sockets, IPv4, and Simple Client/Server Programming,
introduces you to Python's core networking library with varioussmall tasks and enables you to create your first client-server
application
Chapter 2, Multiplexing Socket I/O for Better Performance,
discusses various useful techniques for scaling your client/serverapplications with default and third-party libraries
Chapter 3, IPv6, Unix Domain Sockets, and Network Interfaces,
focuses more on administering your local machine and lookingafter your local area network
Chapter 4, Programming with HTTP for the Internet, enables you to
create a mini command-line browser with various features such assubmitting web forms, handling cookies, managing partial
downloads, compressing data, and serving secure content over
HTTPS
Chapter 5, Email Protocols, FTP, and CGI Programming, brings
you the joy of automating your FTP and e-mail tasks such as
manipulating your Gmail account, and reading or sending emailsfrom a script or creating a guest book for your web application Welearn to write email clients with SMTP and POP3
Chapter 6, Programming Across Machine Boundaries, gives you a
taste of automating your system administration and deploymenttasks over SSH You can run commands, install packages, or set upnew websites remotely from your laptop