45,59 €
Discover practical solutions for a wide range of real-world network programming tasks
This book is for network engineers, system/network administrators, network programmers, and even web application developers who want to solve everyday network-related problems. If you are a novice, you will develop an understanding of the concepts as you progress with this book.
Python Network Programming Cookbook - Second Edition highlights the major aspects of network programming in Python, starting from writing simple networking clients to developing and deploying complex Software-Defined Networking (SDN) and Network Functions Virtualization (NFV) systems. It creates the building blocks for many practical web and networking applications that rely on various networking protocols. It presents the power and beauty of Python to solve numerous real-world tasks in the area of network programming, network and system administration, network monitoring, and web-application development.
In this edition, you will also be introduced to network modelling to build your own cloud network. You will learn about the concepts and fundamentals of SDN and then extend your network with Mininet. Next, you'll find recipes on Authentication, Authorization, and Accounting (AAA) and open and proprietary SDN approaches and frameworks. You will also learn to configure the Linux Foundation networking ecosystem and deploy and automate your networks with Python in the cloud and the Internet scale.
By the end of this book, you will be able to analyze your network security vulnerabilities using advanced network packet capture and analysis techniques.
This book follows a practical approach and covers major aspects of network programming in Python. It provides hands-on recipes combined with short and concise explanations on code snippets. This book will serve as a supplementary material to develop hands-on skills in any academic course on network programming. This book further elaborates network softwarization, including Software-Defined Networking (SDN), Network Functions Virtualization (NFV), and orchestration. We learn to configure and deploy enterprise network platforms, develop applications on top of them with Python.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 435
Veröffentlichungsjahr: 2017
BIRMINGHAM - MUMBAI
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 the case 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, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its 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 information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: March 2014
Second edition: August 2017
Production reference: 1080817
ISBN 978-1-78646-399-9
www.packtpub.com
Authors
Pradeeban Kathiravelu
Dr. M. O. Faruque Sarker
Copy Editors
Safis Editing
Juliana Nair
Reviewers
Dr. S. Gowrishankar
Michael Bright
Project Coordinator
Judie Jose
Commissioning Editor
Kartikey Pandey
Proofreader
Safis Editing
Acquisition Editor
Rahul Nair
Indexer
Aishwarya Gangawane
Content Development Editor
Abhishek Jadhav
Graphics
Kirk D'Penha
Technical Editor
Mohd Riyan Khan
Production Coordinator
Aparna Bhagat
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 de Louvain, 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 in multi-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 of Technology, Sweden. He also holds a first class bachelor of science in engineering (Hons) degree, majoring in computer science and engineering, from the University of Moratuwa, Sri Lanka. His research interests include Software-Defined Networking (SDN), distributed systems, cloud computing, web services, big data in biomedical 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 student and as a mentor.
Pradeeban has published several conference papers and co-authored a few book chapters. He has also worked on OpenDaylight Cookbook and LearningOpenDaylight as a technical reviewer. Python NetworkProgramming Cookbook, Second Edition (2017) is his first book as an author, and he is quite excited about it.
Dr. M. O. Faruque Sarker is a software architect based in London, UK, where he has been shaping various Linux and open source software solutions, mainly on cloud computing platforms, for commercial companies, educational institutions, and multinational consultancies. Over the past 10 years, he has been leading 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 to work on the Google Summer of Code (2009/2010) programs for contributing 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 College London. He takes an active interest in cloud computing, software security, intelligent systems, and child-centric education. He lives in East London with his wife, Shahinur, and daughter, Ayesha.
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 software engineering and BE in computer science and engineering from Visvesvaraya Technological University (VTU), Belagavi, Karnataka, India in the year 2005 and 2003 respectively.
From 2011 to 2014 he worked as a senior research scientist and tech lead at Honeywell Technology Solutions, Bengaluru, Karnataka, India.
He has published several papers in various reputed international journals and conferences. He is serving as an editor and reviewer for various prestigious international journals. He is also a member of IEEE, ACM, CSI, and ISTE.
He has delivered many keynote addresses and invited talks throughout India on a variety of subjects related to computer science and engineering. He was instrumental in organizing several conferences, workshops, and seminars. He has also served on the panel of a number of academic bodies of universities and autonomous 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 implications. Specifically, he is interested in the applications of Machine Learning, Data Mining, and Big Data Analytics in Healthcare.
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 a co-organizer of the Docker and FOSS Meetup groups. He has a keen interest in Container, Orchestration, and Unikernel technologies on which he has presented and run training tutorials in several 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 and pre-sales consulting roles.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.comand as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] 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.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1786463997.
If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
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
Sockets, IPv4, and Simple Client/Server Programming
Introduction
Printing your machine's name and IPv4 address
Getting ready
How to do it...
How it works...
Retrieving a remote machine's IP address
How to do it...
How it works...
Converting an IPv4 address to different formats
How to do it...
How it works...
Finding a service name, given the port and protocol
Getting ready
How to do it...
How it works...
Converting integers to and from host to network byte order
How to do it...
How it works...
Setting and getting the default socket timeout
How to do it...
How it works...
Handling socket errors gracefully
How to do it...
How it works...
Modifying a socket's send/receive buffer sizes
How to do it...
How it works...
Changing a socket to the blocking/non-blocking mode
How to do it...
How it works...
Reusing socket addresses
How to do it...
How it works...
Printing the current time from the internet time server
Getting ready
How to do it...
How it works...
Writing an SNTP client
How to do it...
How it works...
Writing a simple TCP echo client/server application
How to do it...
How it works...
Writing a simple UDP echo client/server application
How to do it...
How it works...
Multiplexing Socket I/O for Better Performance
Introduction
Using ForkingMixIn in your socket server applications
How to do it...
How it works...
Using ThreadingMixIn in your socket server applications
Getting ready
How to do it...
How it works...
Writing a chat server using select.select
How to do it...
How it works...
Multiplexing a web server using select.epoll
How to do it...
How it works...
Multiplexing an echo server using Diesel concurrent library
Getting ready
How to do it...
How it works...
IPv6, Unix Domain Sockets, and Network Interfaces
Introduction
Forwarding a local port to a remote host
How to do it...
How it works...
Pinging hosts on the network with ICMP
Getting ready
How to do it...
How it works...
Waiting for a remote network service
How to do it...
How it works...
Enumerating interfaces on your machine
Getting ready
How to do it...
How it works...
Finding the IP address for a specific interface on your machine
Getting ready
How to do it...
How it works...
Finding whether an interface is up on your machine
Getting ready
How to do it...
How it works...
Detecting inactive machines on your network
Getting ready
How to do it...
How it works...
Performing a basic IPC using connected sockets (socketpair)
Getting ready
How to do it...
How it works...
Performing IPC using Unix domain sockets
How to do it...
How it works...
Finding out if your Python supports IPv6 sockets
Getting ready
How to do it...
How it works...
Extracting an IPv6 prefix from an IPv6 address
How to do it...
How it works...
Writing an IPv6 echo client/server
How to do it...
How it works...
Programming with HTTP for the Internet
Introduction
Downloading data from an HTTP server
How to do it...
How it works...
Serving HTTP requests from your machine
How to do it...
How it works...
Extracting cookie information after visiting a website
How to do it...
How it works...
Submitting web forms
Getting ready
How to do it...
How it works...
Sending web requests through a proxy server
Getting ready
How to do it...
How it works...
Checking whether a web page exists with the HEAD request
How to do it...
How it works...
Spoofing Mozilla Firefox in your client code
How to do it...
How it works...
Saving bandwidth in web requests with the HTTP compression
How to do it...
How it works...
Writing an HTTP fail-over client with resume and partial downloading
How to do it...
How it works...
Writing 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...
Email Protocols, FTP, and CGI Programming
Introduction
Listing the files in a remote FTP server
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...
How it works...
Emailing your current working directory as a compressed ZIP file
Getting ready
How to do it...
How it works...
See also
Downloading your Google email with POP3
Getting ready
How to do it...
How it works...
Checking your remote email with IMAP
Getting ready
How to do it...
How it works...
Sending an email with an attachment via Gmail SMTP server
Getting ready
How to do it...
How it works...
Writing a guestbook for your (Python-based) web server with CGI
Getting ready
How to do it...
How it works...
Finding the mail server from an email address
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...
Programming Across Machine Boundaries
Introduction
Executing 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...
How it works...
Working with Web Services – XML-RPC, SOAP, and REST
Introduction
Querying a local XML-RPC server
Getting ready
How to do it...
How it works...
Writing a multithreaded, multicall XML-RPC server
How to do it...
How it works...
Running an XML-RPC server with a basic HTTP authentication
How to do it...
How it works...
Collecting some photo information from Flickr using REST
How to do it...
How it works...
Searching for SOAP methods from an Amazon S3 web service
Getting ready
How to do it...
How it works...
Searching Amazon for books through the product search API
Getting ready
How to do it...
How it works...
Creating RESTful web applications with Flask
Getting ready
How to do it...
How it works...
Network Monitoring and Security
Introduction
Sniffing packets on your network
Getting ready
How to do it...
How it works...
Saving packets in the pcap format using the pcap dumper
How to do it...
How it works...
Adding an extra header in HTTP packets
How to do it...
How it works...
Scanning the ports of a remote host
How to do it...
How it works...
Customizing the IP address of a packet
How to do it...
How it works...
Replaying traffic by reading from a saved pcap file
How to do it...
How it works...
Scanning the broadcast of packets
How to do it...
How it works...
Network Modeling
Introduction
Simulating networks with ns-3
Getting ready
How to do it...
How 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...
How it works...
Extending Mininet to emulate containers
Getting ready
How to do it...
How it works...
Getting Started with SDN
Introduction
SDN emulation with Mininet
Getting ready
How to do it...
How it works...
Developing Software-Defined Networks with OpenDaylight controller
Getting ready
How to do it...
How it works...
Developing Software-Defined Networks with ONOS controller
Getting ready
How to do it...
How it works...
Developing Software-Defined Networks with Floodlight controller
Getting ready
How to do it...
How it works...
Developing Software-Defined Networks with Ryu controller
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
Getting ready
How to do it...
How it works...
Authentication, Authorization, and Accounting (AAA)
Introduction
Finding DNS names of a network
Getting 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...
How 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...
Throttling requests with RequestsThrottler
Getting ready
How to do it...
How it works...
Open and Proprietary Networking Solutions
Introduction
Configuring Red PNDA
Getting ready
How to do it...
How it works...
Configuring VMware NSX for vSphere 6.3.2
Getting ready
How to do it...
How it works...
Configuring Juniper Contrail Server Manager
Getting ready
How to do it...
How it works...
Configuring OpenContrail controller
Getting ready
How to do it...
How it works...
Configuring OpenContrail cluster
How to do it...
How it works...
Interacting with devices running Cisco IOS XR
Getting ready
How to do it...
How it works...
Collaborating with Cisco Spark API
Getting ready
How to do it...
How it works...
NFV and Orchestration – A Larger Ecosystem
Introduction
Building VNFs with OPNFV
Getting ready
How to do it...
How 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...
Controlling drones with a wireless network
Getting ready
How to do it...
How it works...
Creating PNDA clusters
Getting ready
How to do it...
How it works...
Programming the Internet
Introduction
Checking a website status
Getting ready
How to do it...
How it works...
Benchmarking BGP implementations with bgperf
Getting ready
How to do it...
How it works...
BGP with ExaBGP
Getting ready
How to do it...
Looking glass implementations with Python
Getting ready
How to do it...
How it works...
Understanding the internet ecosystem with Python
Getting ready
How to do it...
How it works...
Establishing BGP connections with yabgp
Getting ready
How to do it...
How it works...
It 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 industry and network softwarization. The widespread use of Software-Defined Networking (SDN), Network Functions Virtualization (NFV), and orchestration have been addressed in detail in the latter chapters while the first eight chapters were taken from the first edition, improved with a few new recipes based on the feedback from 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 to develop various scripts for performing real-world tasks on network and system administration, web application development, interacting with your local and remote network, low-level network packet capture and analysis, and so on. The primary focus of this book 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 is also 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 for better performance. This book introduces you to a bunch of open-source, third-party Python libraries, which are ideal to be used in various use cases. We elaborate in detail the configurations of complex networking systems with helpful hints to ensure that the reader can follow them without getting stuck.
We hope you will enjoy the recipes presented in this book and extend them to make them even more powerful and enjoyable.
Chapter 1, Sockets, IPv4, and Simple Client/Server Programming, introduces you to Python's core networking library with various small 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/server applications with default and third-party libraries.
Chapter 3, IPv6, Unix Domain Sockets, and Network Interfaces, focuses more on administering your local machine and looking after 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 as submitting 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 emails from a script or creating a guest book for your web application. We learn to write email clients with SMTP and POP3.
Chapter 6, Programming Across Machine Boundaries, gives you a taste of automating your system administration and deployment tasks over SSH. You can run commands, install packages, or set up new websites remotely from your laptop.
Chapter 7, Working with Web Services - XML-RPC, SOAP, and REST, introduces you to various API protocols such as XML-RPC, SOAP, and REST. You can programmatically ask any website or web service for information and interact with them. For example, you can search for products on Amazon or Google.
Chapter 8, Network Monitoring and Security, introduces you to various techniques for capturing, storing, analyzing, and manipulating network packets. This encourages you to go further to investigate your network security issues using concise Python scripts.
Chapter 9, Network Modeling, introduces you to the world of network simulations and emulations. You learn to simulate networks with NS-3, and emulate networking systems with Mininet and its extensions.
Chapter 10, Getting Started with SDN, discusses the enterprise SDN controllers, configuring them to use in Software-Defined Networks. We learn to develop SDN visually with MiniEdit, and configure the networks with OpenDaylight, ONOS, Floodlight, Ryu, and POX controllers.
Chapter 11, Authentication, Authorization, and Accounting (AAA), introduces how the networks are secured, and discusses configuring LDAP clients with Python, accounting aspects of the network, and authentication and access of network services.
Chapter 12, Open and Proprietary Networking Solutions, discusses in detail, configuring large-scale enterprise networking projects, including a few projects from Cisco, Juniper, VMware, and the Linux Foundation.
Chapter 13, NFV and Orchestration - A Larger Ecosystem, discusses configuring complex NFV and orchestration systems of the Linux Foundation, such as OPNFV, DPDK, SNAS.io, Dronekit, and PNDA. We elaborate the use of Python in these complex systems.
Chapter 14, Programming the Internet, presents you various Python libraries for BGP protocol and implementations developed for the internet scale. We learn to use and benchmark libraries such as exabgp and yabgp, and also discuss the looking glass implementations with Python.
You need a working PC or laptop, preferably with a modern Linux operating system. The installation instructions are written and tested on Ubuntu 16.04 LTS and would work on any recent Debian-based Linux operating system without modification. We developed for Python 3. However, we have maintained backward-compatibility with Python 2 in our recipes as much as we can. On the other hand, some open source projects used in this book do not yet support Python 3. So, ideally, you will need both Python 2 and Python 3 to test all the recipes in this book.
Most of the recipes in this book will run on other platforms such as Windows and Mac OS with some changes in the configuration steps. Some of the recipes require two or more computers in a cluster to test the distributed systems. You may use Amazon Web Services (AWS) to initiate a cluster inside a single placement group to test these recipes.
You also need a working internet connection to install the third-party software libraries mentioned with respective recipes. If you do not have a stable or continuous internet connection, you can download the third-party libraries and install them in one go. However, it is highly recommended to test some of these recipes with the internet connection, as it would make the configuration task minimal and more interesting, than having to download a bulk of software in bunch. Moreover, testing the application in an AWS cluster would certainly require the internet connectivity.
The following is a list of the Python third-party libraries with their download URLs:
ntplib:
https://pypi.python.org/pypi/ntplib/
diesel:
https://pypi.python.org/pypi/diesel/
nmap:
https://pypi.python.org/pypi/python-nmap
scapy:
https://pypi.python.org/pypi/scapy
netifaces:
https://pypi.python.org/pypi/netifaces/
netaddr:
https://pypi.python.org/pypi/netaddr
pyopenssl:
https://pypi.python.org/pypi/pyOpenSSL
pygeocoder:
https://pypi.python.org/pypi/pygocoder
pyyaml:
https://pypi.python.org/pypi/PyYAML
requests:
https://pypi.python.org/pypi/requests
feedparser:
https://pypi.python.org/pypi/feedparser
paramiko:
https://pypi.python.org/pypi/paramiko/
fabric:
https://pypi.python.org/pypi/Fabric
supervisor:
https://pypi.python.org/pypi/supervisor
xmlrpclib:
https://pypi.python.org/pypi/xmlrpclib
SOAPpy:
https://pypi.python.org/pypi/SOAPpy
bottlenose:
https://pypi.python.org/pypi/bottlenose
construct:
https://pypi.python.org/pypi/construct/
libpcap:
https://pypi.python.org/pypi/pcap
setup tools:
https://pypi.python.org/pypi/setuptools
exabgp:
https://pypi.python.org/pypi/exabgp
traixroute:
https://pypi.python.org/pypi/traixroute
dronekit:
https://pypi.python.org/pypi/dronekit
dronekit-sitl:
https://pypi.python.org/simple/dronekit-sitl/
ryu:
https://pypi.python.org/pypi/ryu
Flask:
https://pypi.python.org/pypi/Flask
smtpd:
https://pypi.python.org/pypi/secure-smtpd
twisted:
https://pypi.python.org/pypi/Twisted
tornado:
https://pypi.python.org/pypi/tornado
dnspython:
https://pypi.python.org/pypi/dnspython
ldap3:
https://pypi.python.org/pypi/ldap3
Eve:
https://pypi.python.org/pypi/Eve
RequestsThrottler:
https://pypi.python.org/pypi/RequestsThrottler
PyNSXv:
https://pypi.python.org/pypi/pynsxv
vmware-nsx:
https://pypi.python.org/pypi/vmware-nsx
Other software needed to run some recipes are as follows:
postfix:
http://www.postfix.org/
openssh server:
http://www.openssh.com/
mysql server:
http://downloads.mysql.com/
apache2:
http://httpd.apache.org/download.cgi/
virtualenv:
https://virtualenv.pypa.io/
filezilla:
https://filezilla-project.org/
vsftpd:
https://security.appspot.com/vsftpd.html
telnetd:
telnetd.sourceforge.net/
curl:
https://curl.haxx.se/
NS-3:
https://www.nsnam.org/ns-3-26/download/
Mininet:
mininet.org/
Ansible:
https://www.ansible.com
/
Git:
https://git-scm.com/
aptitude:
https://www.openhub.net/p/aptitude
Node-ws / wscat:
https://www.npmjs.com/package/wscat
MaxiNet:
https://github.com/MaxiNet/MaxiNet/
Mininet-WiFi:
https://github.com/intrig-unicamp/mininet-wifi
ContainerNet:
https://github.com/containernet/containernet.git
Ant:
ant.apache.org/
Maven:
https://maven.apache.org/
OpenDaylight:
https://www.opendaylight.org/downloads
ONOS:
https://wiki.onosproject.org/display/ONOS/Downloads
Floodlight:
http://www.projectfloodlight.org/download/
POX:
http://github.com/noxrepo/pox
libnl-3-dev:
https://packages.debian.org/sid/libnl-3-dev
libnl-genl-3-dev:
https://packages.debian.org/sid/libnl-genl-3-dev
libnl-route-3-dev:
https://packages.debian.org/sid/libnl-route-3-dev
pkg-config:
https://www.freedesktop.org/wiki/Software/pkg-config/
python-tz:
pytz.sourceforge.net/
libpcap-dev:
https://packages.debian.org/libpcap-dev
libcap2-dev:
https://packages.debian.org/jessie/libcap2-dev
wireshark:
https://www.wireshark.org/
Juniper Contrail:
http://www.juniper.net/support/downloads/?p=contrail#sw
OpenContrail Controller:
https://github.com/Juniper/contrail-controller
Contrail Server Manager:
https://github.com/Juniper/contrail-server-manager.git
VMWare NSX for vSphere 6.3.2:
https://my.vmware.com/group/vmware/details?downloadGroup=NSXV_632_OSS&productId=417
OPNFV Compass:
https://wiki.opnfv.org/display/compass4nfv/Compass4nfv
OPNFV SDNVPN:
https://wiki.opnfv.org/display/sdnvpn/SDNVPN+project+main+page
libpcap-dev:
https://packages.debian.org/libpcap-dev
DPDK:
http://dpdk.org/download
SNAS.io:
http://www.snas.io/
pnda.io:
http://pnda.io/
bgperf:
https://github.com/pradeeban/bgperf.git
swig:
www.swig.org/
yabgp:
https://github.com/smartbgp/yabgp
Virtualbox:
https://www.virtualbox.org/wiki/VirtualBox
Vagrant:
https://www.vagrantup.com/
RED PNDA:
https://github.com/pndaproject/red-pnda
Apache ZooKeeper:
https://zookeeper.apache.org/
Apache Cassandra:
http://cassandra.apache.org/
RabbitMQ:
https://www.rabbitmq.com/
pyIOSXR:
https://github.com/fooelisa/pyiosxr
Cisco Spark API:
https://github.com/CiscoDevNet/ciscosparkapi
If you are a network programmer, system/network administrator, or a web application developer, this book is ideal for you. You should have a basic familiarity with the Python programming language and TCP/IP networking concepts. However, if you are a novice, you will develop an understanding of the concepts as you progress with this book. This book will serve as supplementary material for developing hands-on skills in any academic course on network programming.
In this book, you will find several headings that appear frequently (Getting ready, How to do it…, How it works…, There's more…, and See also). To give clear instructions on how to complete a recipe, we use these sections as follows:
This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors .
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the
SUPPORT
tab at the top.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on
Code Download
.
You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account. Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Python-Network-Programming-Cookbook-Second-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/PythonNetworkProgrammingCookbookSecondEdition_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected] with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.
In this chapter, we will cover the following recipes:
Printing your machine's name and IPv4 address
Retrieving a remote machine's IP address
Converting an IPv4 address to different formats
Finding a service name, given the port and protocol
Converting integers to and from host to network byte order
Setting and getting the default socket timeout
Handling socket errors gracefully
Modifying a socket's send/receive buffer size
Changing a socket to the blocking/non-blocking mode
Reusing socket addresses
Printing the current time from the internet time server
Writing an SNTP client
Writing a simple TCP echo client/server application
Writing a simple UDP echo client/server application
This chapter introduces Python's core networking library through some simple recipes. Python's socket module has both class-based and instances-based utilities. The difference between a class-based and instance-based method is that the former doesn't need an instance of a socket object. This is a very intuitive approach. For example, in order to print your machine's IP address, you don't need a socket object. Instead, you can just call the socket's class-based methods. On the other hand, if you need to send some data to a server application, it is more intuitive that you create a socket object to perform that explicit operation. The recipes presented in this chapter can be categorized into three groups as follows:
In the first few recipes, the class-based utilities have been used in order to extract some useful information about host, network, and any target service.
After that, some more recipes have been presented using the instance-based utilities. Some common socket tasks, including manipulating the socket timeout, buffer size, and blocking mode has been demonstrated.
Finally, both class-based and instance-based utilities have been used to construct some clients, which perform some practical tasks, for example, synchronizing the machine time with an internet server or writing a generic client/server script.
You can use these demonstrated approaches to write your own client/server application.
Sometimes, you need to quickly discover some information about your machine, for example, the hostname, IP address, number of network interfaces, and so on. This is very easy to achieve using Python scripts.
You need to install Python on your machine before you start coding. Python comes preinstalled in most of the Linux distributions. For Microsoft Windows operating systems, you can download binaries from the Python website: http://www.python.org/download/.
Currently, Python 3.x is released in addition to Python 2.x. Many of the current Linux distributions and macOS versions are still shipping Python 2 by default. However, some ship both of them.
Download the relevant installer for your operating system and the relevant version based on whether your operating system is 32 bit or 64 bit.
You may consult the documentation of your operating system to check and review your Python setup. After installing Python on your machine, you can try opening the Python interpreter from the command line by typing python. This will show the interpreter prompt, >>>, which should be similar to the following output:
~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
The import socket statement imports one of Python's core networking libraries. Then, we use the two utility functions, gethostname() and gethostbyname(host_name). You can type help(socket.gethostname) to see the online help information from within the command line. Alternatively, you can type the following address in your web browser at http://docs.python.org/3/library/socket.html. You can refer to the following code:
gethostname(...) gethostname() -> string Return the current host name. gethostbyname(...) gethostbyname(host) -> address Return the IP address (a string of the form '255.255.255.255') for a host.
The first function takes no parameter and returns the current or localhost name. The second function takes a single hostname parameter and returns its IP address.
Sometimes, you need to translate a machine's hostname into its corresponding IP address, for example, a quick domain name lookup. This recipe introduces a simple function to do that.
When you would like to deal with low-level network functions, sometimes, the usual string notation of IP addresses are not very useful. They need to be converted to the packed 32-bit binary formats.
In this recipe, the two IP addresses have been converted from a string to a 32-bit packed format using a for-in statement. Additionally, the Python hexlify function is called from the binascii module. This helps to represent the binary data in a hexadecimal format.
If you would like to discover network services, it may be helpful to determine what network services run on which ports using either the TCP or UDP protocol.
If you know the port number of a network service, you can find the service name using the getservbyport() socket class function from the socket library. You can optionally give the protocol name when calling this function.
In this recipe, the for-in statement is used to iterate over a sequence of variables. So for each iteration, we use one IP address to convert them in their packed and unpacked format.
If you ever need to write a low-level network application, it may be necessary to handle the low-level data transmission over the wire between two machines. This operation requires some sort of conversion of data from the native host operating system to the network format and vice versa. This is because each one has its own specific representation of data.
Python's socket library has utilities for converting from a network byte order to host byte order and vice versa. You may want to become familiar with them, for example,
