O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Python Network Programming Cookbook - Second Edition

Book Description

Discover practical solutions for a wide range of real-world network programming tasks

About This Book
  • Solve real-world tasks in the area of network programming, system/networking administration, network monitoring, and more.
  • Familiarize yourself with the fundamentals and functionalities of SDN
  • Improve your skills to become the next-gen network engineer by learning the various facets of Python programming
Who This Book Is For

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.

What You Will Learn
  • Develop TCP/IP networking client/server applications
  • Administer local machines' IPv4/IPv6 network interfaces
  • Write multi-purpose efficient web clients for HTTP and HTTPS protocols
  • Perform remote system administration tasks over Telnet and SSH connections
  • Interact with popular websites via web services such as XML-RPC, SOAP, and REST APIs
  • Monitor and analyze major common network security vulnerabilities
  • Develop Software-Defined Networks with Ryu, OpenDaylight, Floodlight, ONOS, and POX Controllers
  • Emulate simple and complex networks with Mininet and its extensions for network and systems emulations
  • Learn to configure and build network systems and Virtual Network Functions (VNF) in heterogeneous deployment environments
  • Explore various Python modules to program the Internet
In Detail

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.

Style and approach

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.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased 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 code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Conventions
    6. Reader feedback
    7. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Sockets, IPv4, and Simple Client/Server Programming
    1. Introduction
    2. Printing your machine's name and IPv4 address
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Retrieving a remote machine's IP address
      1. How to do it...
      2. How it works...
    4. Converting an IPv4 address to different formats
      1. How to do it...
      2. How it works...
    5. Finding a service name, given the port and protocol
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Converting integers to and from host to network byte order
      1. How to do it...
      2. How it works...
    7. Setting and getting the default socket timeout
      1. How to do it...
      2. How it works...
    8. Handling socket errors gracefully
      1. How to do it...
      2. How it works...
    9. Modifying a socket's send/receive buffer sizes
      1. How to do it...
      2. How it works...
    10. Changing a socket to the blocking/non-blocking mode
      1. How to do it...
      2. How it works...
    11. Reusing socket addresses
      1. How to do it...
      2. How it works...
    12. Printing the current time from the internet time server
      1. Getting ready
      2. How to do it...
      3. How it works...
    13. Writing an SNTP client
      1. How to do it...
      2. How it works...
    14. Writing a simple TCP echo client/server application
      1. How to do it...
      2. How it works...
    15. Writing a simple UDP echo client/server application
      1. How to do it...
      2. How it works...
  3. Multiplexing Socket I/O for Better Performance
    1. Introduction
    2. Using ForkingMixIn in your socket server applications
      1. How to do it...
      2. How it works...
    3. Using ThreadingMixIn in your socket server applications
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Writing a chat server using select.select
      1. How to do it...
      2. How it works...
    5. Multiplexing a web server using select.epoll
      1. How to do it...
      2. How it works...
    6. Multiplexing an echo server using Diesel concurrent library
      1. Getting ready
      2. How to do it...
      3. How it works...
  4. IPv6, Unix Domain Sockets, and Network Interfaces
    1. Introduction
    2. Forwarding a local port to a remote host
      1. How to do it...
      2. How it works...
    3. Pinging hosts on the network with ICMP
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Waiting for a remote network service
      1. How to do it...
      2. How it works...
    5. Enumerating interfaces on your machine
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Finding the IP address for a specific interface on your machine
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Finding whether an interface is up on your machine
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Detecting inactive machines on your network
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Performing a basic IPC using connected sockets (socketpair)
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Performing IPC using Unix domain sockets
      1. How to do it...
      2. How it works...
    11. Finding out if your Python supports IPv6 sockets
      1. Getting ready
      2. How to do it...
      3. How it works...
    12. Extracting an IPv6 prefix from an IPv6 address
      1. How to do it...
      2. How it works...
    13. Writing an IPv6 echo client/server
      1. How to do it...
      2. How it works...
  5. Programming with HTTP for the Internet
    1. Introduction
    2. Downloading data from an HTTP server
      1. How to do it...
      2. How it works...
    3. Serving HTTP requests from your machine
      1. How to do it...
      2. How it works...
    4. Extracting cookie information after visiting a website
      1. How to do it...
      2. How it works...
    5. Submitting web forms
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Sending web requests through a proxy server
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Checking whether a web page exists with the HEAD request
      1. How to do it...
      2. How it works...
    8. Spoofing Mozilla Firefox in your client code
      1. How to do it...
      2. How it works...
    9. Saving bandwidth in web requests with the HTTP compression
      1. How to do it...
      2. How it works...
    10. Writing an HTTP fail-over client with resume and partial downloading
      1. How to do it...
      2. How it works...
    11. Writing a simple HTTPS server code with Python and OpenSSL
      1. Getting ready
      2. How to do it...
      3. How it works...
    12. Building asynchronous network applications with Twisted
      1. Getting ready
      2. How to do it...
      3. How it works...
    13. Building asynchronous network applications with Tornado
      1. Getting ready
      2. How to do it...
      3. How it works...
    14. Building concurrent applications with Tornado Future
      1. Getting ready
      2. How to do it...
      3. How it works...
  6. Email Protocols, FTP, and CGI Programming
    1. Introduction
    2. Listing the files in a remote FTP server
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. Common error
    3. Uploading a local file to a remote FTP server
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Emailing your current working directory as a compressed ZIP file
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Downloading your Google email with POP3
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Checking your remote email with IMAP
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Sending an email with an attachment via Gmail SMTP server
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Writing a guestbook for your (Python-based) web server with CGI
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Finding the mail server from an email address
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Writing a simple SMTP server
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Writing a secure SMTP client using TLS
      1. Getting ready
      2. How to do it...
      3. How it works...
    12. Writing an email client with POP3
      1. Getting ready
      2. How to do it...
      3. How it works...
  7. Programming Across Machine Boundaries
    1. Introduction
    2. Executing a remote shell command using telnet
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Copying a file to a remote machine by SFTP
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Printing a remote machine's CPU information
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Installing a Python package remotely
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Running a MySQL command remotely
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Transferring files to a remote machine over SSH
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Configuring Apache remotely to host a website
      1. Getting ready
      2. How to do it...
      3. How it works...
  8. Working with Web Services – XML-RPC, SOAP, and REST
    1. Introduction
    2. Querying a local XML-RPC server
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Writing a multithreaded, multicall XML-RPC server
      1. How to do it...
      2. How it works...
    4. Running an XML-RPC server with a basic HTTP authentication
      1. How to do it...
      2. How it works...
    5. Collecting some photo information from Flickr using REST
      1. How to do it...
      2. How it works...
    6. Searching for SOAP methods from an Amazon S3 web service
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Searching Amazon for books through the product search API
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Creating RESTful web applications with Flask
      1. Getting ready
      2. How to do it...
      3. How it works...
  9. Network Monitoring and Security
    1. Introduction
    2. Sniffing packets on your network
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Saving packets in the pcap format using the pcap dumper
      1. How to do it...
      2. How it works...
    4. Adding an extra header in HTTP packets
      1. How to do it...
      2. How it works...
    5. Scanning the ports of a remote host
      1. How to do it...
      2. How it works...
    6. Customizing the IP address of a packet
      1. How to do it...
      2. How it works...
    7. Replaying traffic by reading from a saved pcap file
      1. How to do it...
      2. How it works...
    8. Scanning the broadcast of packets
      1. How to do it...
      2. How it works...
  10. Network Modeling
    1. Introduction
    2. Simulating networks with ns-3
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Emulating networks with Mininet
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Distributed network emulation with MaxiNet
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Emulating wireless networks with Mininet-WiFi
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Extending Mininet to emulate containers
      1. Getting ready
      2. How to do it...
      3. How it works...
  11. Getting Started with SDN
    1. Introduction
    2. SDN emulation with Mininet
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Developing Software-Defined Networks with OpenDaylight controller
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Developing Software-Defined Networks with ONOS controller
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Developing Software-Defined Networks with Floodlight controller
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Developing Software-Defined Networks with Ryu controller
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Developing Software-Defined Networks with POX controller
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Developing Software-Defined Networks visually with MiniEdit
      1. Getting ready
      2. How to do it...
      3. How it works...
  12. Authentication, Authorization, and Accounting (AAA)
    1. Introduction
    2. Finding DNS names of a network
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Finding DNS host information
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Finding DNS resource records
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Making DNS zone transfer
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Querying NTP servers
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Connecting to an LDAP server
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Making LDAP bind
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Reading and writing LDAP
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Authenticating REST APIs with Eve
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Throttling requests with RequestsThrottler
      1. Getting ready
      2. How to do it...
      3. How it works...
  13. Open and Proprietary Networking Solutions
    1. Introduction
    2. Configuring Red PNDA
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Configuring VMware NSX for vSphere 6.3.2
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Configuring Juniper Contrail Server Manager
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Configuring OpenContrail controller
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Configuring OpenContrail cluster
      1. How to do it...
      2. How it works...
    7. Interacting with devices running Cisco IOS XR
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Collaborating with Cisco Spark API
      1. Getting ready
      2. How to do it...
      3. How it works...
  14. NFV and Orchestration – A Larger Ecosystem
    1. Introduction
    2. Building VNFs with OPNFV
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Packet processing with DPDK
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Parsing BMP messages with SNAS.io
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Controlling drones with a wireless network
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Creating PNDA clusters
      1. Getting ready
      2. How to do it...
      3. How it works...
  15. Programming the Internet
    1. Introduction
    2. Checking a website status
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Benchmarking BGP implementations with bgperf
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. BGP with ExaBGP
      1. Getting ready
      2. How to do it...
    5. Looking glass implementations with Python
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Understanding the internet ecosystem with Python
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Establishing BGP connections with yabgp
      1. Getting ready
      2. How to do it...
      3. How it works...