In the previous lesson, you used EEM to automate tasks directly on a router.
EEM is powerful, but the applet runs inside a single device.When you need to manage a fleet of devices from a central place, you write external scripts.
The standard language for that is Python.What Python Talks To
Your Python script does not type CLI commands like a human.
It sends structured requests to a device that exposes an API.
The device replies with structured data, almost always in JSON.Take a look at the diagram below.

Figure 1 – Python is the language. The protocol is the road.
To make things easier, I split the picture into four layers.
You do not need to memorize them.
They are just a mental map I use to walk you through the topic, one piece at a time.Layer 1 – What you write: the Python script itself.
Layer 2 – What you import: the Python library that does the heavy lifting (
requestsorncclient).Layer 3 – What travels: the network protocol on the wire (RESTCONF or NETCONF).
Layer 4 – The device: the Cisco IOS-XE router that receives the request, applies it, and replies.
In the next four sections, you will study each layer one by one.
Then in the last section, you will see exam-style scripts that put all four layers together.Answer the question below
What does the Python script send to a device that exposes an API? (one word, plural)
Answer the question below
In what format does the device reply to a Python script?

Figure 2 – Layer 1: the Python script you write
Layer 1 is the code you actually type.
Variables, lists, dictionaries, loops. The plain Python that holds your logic.
Every exam exhibit you will read is built from the same four blocks.
Master them and you can read any script.Variables and Types
A variable is a name that points to a value.
You assign with a single equal sign.hostname = "R1" ip_address = "192.168.1.1" port = 830 enabled = TrueThree types cover almost every networking value you will read in scripts:
string: text in quotes, like
"R1"integer: a whole number, like
830boolean:
TrueorFalse(capitalized in Python)
Answer the question below
What type is the value assigned to "port" in port = 830?
Lists and Indexing
A list stores multiple values in order.
You wrap them in square brackets.interfaces = ["Gi0/0", "Gi0/1", "Gi0/2", "Gi0/3"] print(interfaces[0]) # first element print(interfaces[-1]) # last elementOutput:
Gi0/0 Gi0/3The first element is index 0, not 1.
Negative indexes count from the end, so[-1]is the last element. This is a favorite exam pattern.Answer the question below
Given devices = ["R1", "R2", "R3"], what does devices[-1] return?
Dictionaries
A dictionary (or dict) stores values associated with keys.
You wrap them in curly braces.device = { "hostname": "R1", "ip": "192.168.1.1", "port": 830 } print(device["hostname"]) print(device["port"])Output:
R1 830You access a value by its key, between square brackets.
If the key does not exist, Python raises aKeyError.Answer the question below
In the dict above, what does device["ip"] return?
Nested Dictionaries
Network APIs almost never return a flat dictionary.
They return dicts inside dicts, often with lists in the middle.device = { "hostname": "R1", "ip": "192.168.1.1", "interfaces": [ {"name": "Gi0/0", "status": "up"}, {"name": "Gi0/1", "status": "down"} ] }To reach a deep value, you chain the brackets in order.
Each[...]moves you one step deeper into the structure.
Figure 3 – Walk the keys, one bracket at a time
Now look at this expression:
status = device["interfaces"][0]["status"] print(status)Read it left to right:
device["interfaces"]returns the list of interface dicts[0]picks the first dict in that list["status"]picks the status key inside that dict
Output:
upMany exam scripts are exactly this pattern.
You see a JSON response, you trace the keys to the value, you pick the right answer.Answer the question below
What does device["interfaces"][1]["name"] return for the dict above?
Loops Over a List of Dicts
When the list contains dictionaries, a for loop lets you process every item in turn.
for iface in device["interfaces"]: print(iface["name"], iface["status"])Output:
Gi0/0 up Gi0/1 downOn every iteration,
ifacetakes the value of the current dict in the list.
Inside the loop, you access its keys exactly like a normal dict.Answer the question below
In the loop above, what is the type of the variable iface on each iteration?

Figure 4 – Layer 2: the Python library that handles the API
Layer 2 is the library you import.
The most common one isrequests.
It does the heavy lifting: opening the HTTPS connection, sending your request, returning the response.
In two lines, you can already talk to a router:import requests response = requests.get("https://192.168.1.1/restconf/data/...")That is it.
The library handles HTTPS, encoding, sockets.
You only deal with theresponseobject that comes back.
And the data inside that response arrives in one specific format you must know: JSON.Two small details in this section trip up most candidates at the exam.
Get them right and the rest of the lesson clicks into place.40 % Complete: you’re making great progress
Ready to pass your CCNP exam?