Python Network Programming

Book Description

Power up your network applications with Python programming

Key Features

  • Master Python skills to develop powerful network applications
  • Grasp the fundamentals and functionalities of SDN
  • Design multi-threaded, event-driven architectures for echo and chat servers

Book Description

This Learning Path highlights major aspects of Python network programming such as writing simple networking clients, creating and deploying SDN and NFV systems, and extending your network with Mininet. You'll also learn how to automate legacy and the latest network devices. As you progress through the chapters, you'll use Python for DevOps and open source tools to test, secure, and analyze your network. Toward the end, you'll develop client-side applications, such as web API clients, email clients, SSH, and FTP, using socket programming.

By the end of this Learning Path, you will have learned how to analyze a network's security vulnerabilities using advanced network packet capture and analysis techniques.

This Learning Path includes content from the following Packt products:

  • Practical Network Automation by Abhishek Ratan
  • Mastering Python Networking by Eric Chou
  • Python Network Programming Cookbook, Second Edition by Pradeeban Kathiravelu, Dr. M. O. Faruque Sarker

What you will learn

  • Create socket-based networks with asynchronous models
  • Develop client apps for web APIs, including S3 Amazon and Twitter
  • Talk to email and remote network servers with different protocols
  • Integrate Python with Cisco, Juniper, and Arista eAPI for automation
  • Use Telnet and SSH connections for remote system monitoring
  • Interact with websites via XML-RPC, SOAP, and REST APIs
  • Build networks with Ryu, OpenDaylight, Floodlight, ONOS, and POX
  • Configure virtual networks in different deployment environments

Who this book is for

If you are a Python developer or a system administrator who wants to start network programming, this Learning Path gets you a step closer to your goal. IT professionals and DevOps engineers who are new to managing network devices or those with minimal experience looking to expand their knowledge and skills in Python will also find this Learning Path useful. Although prior knowledge of networking is not required, some experience in Python programming will be helpful for a better understanding of the concepts in the Learning Path.

Publisher Resources

Download Example Code

Table of Contents

  1. Title Page
  2. Copyright
    1. Python Network Programming
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the authors
    2. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Fundamental Concepts
    1. Network automation
    2. DevOps
    3. Software-defined networking
    4. OpenFlow
    5. Program concepts
      1. Variables
      2. Data types
      3. Decision makers
      4. Loops
      5. Arrays
      6. Functions
      7. Best practices
        1. Readability of a program
        2. Support information
        3. Indentation
        4. Sample best practice example
    6. Language choices (Python/PowerShell)
      1. Writing your first program
        1. PowerShell IDE
        2. Python IDE
    7. Representational State Transfer (REST) framework
    8. Summary
  7. Python for Network Engineers
    1. Python interpreter and data types
    2. Conditions and loops
      1. Nested and multiple conditions
      2. Loops
        1. For next loop
        2. While loop
    3. Writing Python scripts
    4. Functions
      1. Passing arguments from the command line
    5. Python modules and packages
    6. Multithreading for parallel processing
    7. Using Netmiko for SSH and network device interaction
    8. Network automation use case
    9. Summary
  8. Continuous Integration for Network Engineers
    1. Interaction with Splunk
    2. Automation examples on various technology domains
      1. BGP and routing table
      2. Configuring Cisco switchport for access point
      3. Configuring Cisco switchport for IP Phone
      4. Wireless LAN (WLAN)
      5. Access of IP Address Management (IPAM)
    3. Example and use case
      1. Create a web-based pre and post check tool for validations
        1. Step 1 – Create the main HTML file
        2. Step 2 – Create the backend Python code
        3. Step 3 – Create web server based files for the tool
        4. Step 4 – Create server based files for pre and post files comparison
    4. Summary
  9. SDN Concepts in Network Automation
    1. Managing cloud platforms
    2. Programmable network devices
    3. Controller-based network fabric
    4. Network automation tools
    5. Summary
  10. Low-Level Network Device Interactions
    1. The challenges of the CLI
    2. Constructing a virtual lab
      1. Cisco VIRL
        1. VIRL tips
      2. Cisco DevNet and dCloud
      3. GNS3
    3. Python Pexpect library
      1. Pexpect installation
      2. Pexpect overview
      3. Our first Pexpect program
      4. More Pexpect features
      5. Pexpect and SSH
      6. Putting things together for Pexpect
    4. The Python Paramiko library
      1. Installation of Paramiko
      2. Paramiko overview
      3. Our first Paramiko program
      4. More Paramiko features
        1. Paramiko for servers
      5. Putting things together for Paramiko
    5. Looking ahead
      1. Downsides of Pexpect and Paramiko compared to other tools
        1. Idempotent network device interaction
        2. Bad automation speeds bad things up
    6. Summary
  11. APIs and Intent-Driven Networking
    1. Infrastructure as code
      1. Intent-Driven Networking
      2. Screen scraping versus API structured output
      3. Data modeling for infrastructure as code
    2. The Cisco API and ACI
      1. Cisco NX-API
        1. Lab software installation and device preparation
        2. NX-API examples
      2. The Cisco and YANG models
      3. The Cisco ACI
    3. The Python API for Juniper networks
      1. Juniper and NETCONF
        1. Device preparation
        2. Juniper NETCONF examples
      2. Juniper PyEZ for developers
        1. Installation and preparation
        2. PyEZ examples
    4. The Arista Python API
      1. Arista eAPI management
        1. The eAPI preparation
        2. eAPI examples
      2. The Arista Pyeapi library
        1. Pyeapi installation
        2. Pyeapi examples
    5. Vendor-neutral libraries
    6. Summary
  12. The Python Automation Framework – Ansible Basics
    1. A more declarative framework
    2. A quick Ansible example
      1. The control node installation
      2. Running different versions of Ansible from source
      3. Lab setup
      4. Your first Ansible playbook
        1. The public key authorization
        2. The inventory file
        3. Our first playbook
    3. The advantages of Ansible
      1. Agentless
      2. Idempotent
      3. Simple and extensible
      4. Network vendor support
    4. The Ansible architecture
      1. YAML
      2. Inventories
      3. Variables
      4. Templates with Jinja2
    5. Ansible networking modules
      1. Local connections and facts
      2. Provider arguments
    6. The Ansible Cisco example
      1. Ansible 2.5 connection example
    7. The Ansible Juniper example
    8. The Ansible Arista example
    9. Summary
  13. The Python Automation Framework – Beyond Basics
    1. Ansible conditionals
      1. The when clause
      2. Ansible network facts
      3. Network module conditional
    2. Ansible loops
      1. Standard loops
      2. Looping over dictionaries
    3. Templates
      1. The Jinja2 template
      2. Jinja2 loops
      3. The Jinja2 conditional
    4. Group and host variables
      1. Group variables
      2. Host variables
    5. The Ansible Vault
    6. The Ansible include and roles
      1. The Ansible include statement
      2. Ansible roles
    7. Writing your own custom module
      1. The first custom module
      2. The second custom module
    8. Summary
  14. AWS Cloud Networking
    1. AWS setup
      1. AWS CLI and Python SDK
    2. AWS network overview
    3. Virtual private cloud
      1. Route tables and route targets
      2. Automation with CloudFormation
      3. Security groups and the network ACL
      4. Elastic IP
      5. NAT Gateway
    4. Direct Connect and VPN
      1. VPN Gateway
      2. Direct Connect
    5. Network scaling services
      1. Elastic Load Balancing
      2. Route53 DNS service
      3. CloudFront CDN services
    6. Other AWS network services
    7. Summary
  15. Working with Git
    1. Introduction to Git
      1. Benefits of Git
      2. Git terminology
      3. Git and GitHub
    2. Setting up Git
      1. Gitignore
    3. Git usage examples
      1. GitHub example
        1. Collaborating with pull requests
    4. Git with Python
      1. GitPython
      2. PyGitHub
    5. Automating configuration backup
    6. Collaborating with Git
    7. Summary
  16. 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...
  17. 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...
  18. 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...
  19. 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...
  20. 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...
  21. 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...
  22. Working with Web Services – 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...
  23. 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...
  24. 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...
  25. 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...
  26. 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...
  27. 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...
  28. 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...
  29. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product Information

  • Title: Python Network Programming
  • Author(s): Abhishek Ratan, Eric Chou, Pradeeban Kathiravelu, Dr. M. O. Faruque Sarker
  • Release date: January 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781788835466