How to learn Python for networking

If you are into networking and you want to learn Python, your end goal might be to use Python to communicate with a router, switch, firewall, or other network device through an (Application Programming Interfaces) by using a Python script.

Best order to learn

When Python is new to you, don't start attempting to write a Python script that communicates with an API on a network device. This will be a frustrating experience because you require this knowledge:

  • Basic Python syntax about variables, data types, functions, loops, conditionals.
  • Use Python to connect with SSH.
  • Basic knowledge about APIs and their protocols and data formats:
    • Protocols such as HTTP or RPC that are used to communicate between a client (your Python script) and the server (API on the network device).
    • Data formats such as JSON or XML that are used to structure data that you exchange between your Python script and the network device.

Approach

In a nutshell, this is how to approach this:

Basics

Learn the basics of Python first. You'll need to learn what strings, variables, functions, operators, etc. are. You can do this by going through our Python course.

Try to write some basic Python scripts that are "self-contained". I'm talking about simple scripts that don't communicate with anything external. Here are some examples:

  • Print a greeting message

    • Write a program that prints "Hello, I learn Python!" to the console.
  • Simple arithmetic with network values

    • Write a program that takes two network bandwidth values (in Mbps) as input and prints their sum and difference.
  • List operations with IP addresses

    • Write a program that creates a list of five IP addresses and then prints the highest and lowest IP address in the list.
  • String editing for device names

    • Write a program that takes a device name as input and capitalizes the first letter.
  • Dictionary operations for device information

    • Write a program that creates a dictionary with three key-value pairs representing device information (e.g., hostname, IP address, MAC address) and then prints each key and its value.
  • File I/O for configuration files

    • Write a program that reads a CSV text file containing hostnames and IP addresses and prints its contents to the console.
  • Basic calculator for network values

    • Write a program that takes two network values (e.g., bandwidth, latency) and multiplies them.
  • Temperature conversion for device sensors

    • Write a program that converts temperatures between Celsius and Fahrenheit for network device sensors.
  • Simple password Generator for device access

    • Write a program that generates a random password with a given length for securing device access.

SSH

Now you know the basics of Python, see if you can use it to communicate with a network device through SSH.

You might already know SSH because we use this for the CLI so this is a great way to combine something you already know (SSH) with something new (Python).

APIs

Basics

Learn the basics of APIs such as REST API:

  • What is an API.
  • What protocols can we use to communicate with an API such as HTTP or RPC.
  • What data formats such as JSON or XML can we use.

Start with simple APIs that don't require authentication. Here is a list with public free APIs:

Apipheny.io

Once you are able to communicate with an API, see if you can communicate with an API that requires authentication. There are some free test APIs out there such as NewsAPI.

Network devices

This is your end goal. See if you can use Python to communicate with a network device. Start simple, see if you can extract some information, the equivalent of a CLI show command. When you can get this working, see if you can make changes to the configuration.

Large language models (LLMs)

LLMs are excellent to learn Python. Here are some examples of LLMs:

  • Claude from Anthropic
  • GPT from OpenAI
  • Gemini from Google

You need to know how to use them though. If you ask "write python code to communicate with Cisco router through API" then it usually produces pretty good code but you'll have no idea what it does or how to troubleshoot it.

Instead, ask the LLM to generate examples for you to practice Python. Ask it to explain everything needed to communicate with an API, and let it explain everything step by step. Use it as a teacher, not as something to do your homework.