ITT 440 Network Programming - Chapter 7: Network Programming in Python

Introduction to Python

  • Python is a popular programming language created by Guido van Rossum and released in 1991.
  • It is an interpreted, open-source, high-level, and general-purpose programming language.
  • Python's design emphasizes code readability, using significant whitespace.
  • It is used for:
    • Web development (server-side)
    • Software development
    • Mathematics
    • System scripting

Why Python?

  • Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.).
  • It has a simple syntax similar to the English language.
  • Python allows developers to write programs with fewer lines of code compared to some other languages.
  • It runs on an interpreter system, meaning code can be executed as soon as it is written, enabling quick prototyping.
  • Python can be treated in a procedural, object-oriented, or functional way.

Using Python

  • The most recent major version is Python 3; Python 2 is no longer officially supported.
  • Installing Python in Ubuntu/Linux:
    sudoaptinstallpython3sudo apt install python3
  • Installing PIP (the recommended tool for installing Python packages):
    sudoaptinstallpython3pipsudo apt install python3-pip

Comparison Between C and Python Language

  • C Language:
    • Fast execution speed
  • Python Language:
    • Easy to learn
    • Open source with easy access to libraries

Python Network Programming

Socket Programming Using Python

  • Like other high-level programming languages, Python has sockets, and the socket API is used to send messages across a network.
  • Python’s socket module provides an interface to the Berkeley sockets API.
  • Python provides a convenient and consistent API that maps directly to these system calls and their C counterparts.
  • Many modules are available that implement higher-level Internet protocols like HTTP and SMTP.

TCP Connection: Server Code

  • The server binds the socket to port 8888, listens for incoming connections, and sends a message (in byte type) back when a connection is initiated.

TCP Connection: Client Code

  • The client creates a socket and initiates a connection with the server, then prints the response from the server.

Python's Socket-Related Functions:

  • socket
  • bind
  • listen
  • accept
  • connect, connect_ex
  • send, recv
  • close

Python for Network Engineer

Automation Using Python

  • Python can automate tasks usually done manually by network or system engineers.
  • Examples of tasks that can be automated:
    • Backing up devices’ configurations
    • Checking device status
    • Updating configurations
  • Useful Python modules for automation:
    • Paramiko: A generic SSH module for automating specific SSH tasks.
    • Netmiko: Broader and well-optimized for managing network devices such as switches and routers, usually accessing network devices via SSH.
    • Requests: Allows sending HTTP requests using Python, commonly used to interact with REST APIs.

Network Automation Scenario

  • Script Server (Netmiko/Ansible/Jenkins) interacts with network devices such as routers, switches, bare-metal servers, virtual machines, and containers within the management network.
  • Automated tasks include:
    • Configuration backup
    • Configuration update
    • Retrieving information
    • Change requests
    • Software updates
    • Interactions via REST API

Network Automation Using Netmiko

  • Netmiko is a multi-vendor library to simplify CLI connections to network devices.
  • It accesses network device terminals using either SSH or Telnet to execute the necessary commands.
  • To install Netmiko, use the following command:
    sudoaptinstallpython3netmikosudo apt install python3-netmiko

Interacting with REST API Using Requests

  • Python has modules that can interact with REST APIs for network automation.
  • A REST API (RESTful API) is an application programming interface that conforms to REST architectural constraints, allowing interaction with RESTful web services.
  • Using APIs, we can retrieve, update, add, and delete configurations or information from external systems or devices.
  • The requests module allows sending HTTP requests using Python.
  • The HTTP request returns a Response Object with all the response data (content, encoding, status, etc.).