You can read Python data structures and you know the HTTP rules.
Now you put them together against the kind of code you will see in an exam exhibit.Remember three RESTCONF principles:
HTTPS on TCP port 443
JSON format for data input and output
A long URL pointing to a specific configuration item on the router

Figure 1 – Lab topology
PC1 runs Python with the
requestslibrary installed.
It reaches Cisco IOS-XE router R1 (192.168.1.1) over TCP port 443 to send RESTCONF traffic.Script 1 — Simple GET Request
Here is the smallest networking script you can write.
It asks R1 for itsGigabitEthernet0/1configuration over RESTCONF:import requests import urllib3 urllib3.disable_warnings() url = "https://192.168.1.1/restconf/data/Cisco-IOS-XE-native:native/interface/GigabitEthernet=0%2F1" headers = {"Accept": "application/yang-data+json"} response = requests.get(url, headers=headers, auth=("admin", "cisco123"), verify=False) print(response.status_code) print(response.json())Walk through it line by line:
import requests: Loads the HTTP library.urllib3.disable_warnings()andverify=False: Suppress SSL certificate warnings (acceptable in a lab, not in production).url: Points at one specific item on the router (GigabitEthernet0/1).headers: Tells R1 you want JSON, not XML.requests.get(...): Sends an HTTPS GET request with your credentials.auth=("admin", "cisco123"): Passes HTTP Basic Auth credentials to R1, exactly like an SSH login.response.json(): Converts the JSON response body into a Python dict so you can print it.
If R1 accepts the request,
response.status_codereturns200and the parsed dictionary appears.Answer the question below
What HTTP method does this script use to read the interface configuration?
Answer the question below
What is the type of the value returned by response.json() in this script?
Answer the question below
Which HTTP status code would response.status_code show if R1 accepts the request?
Script 1 fetched one interface and printed the whole dictionary.
Real scripts dig into that dictionary, and they plan for the day R1 does not answer.Script 2 — Parse and Loop Through Interfaces
This is the kind of script you will see in an exam exhibit.
It fetches every interface from R1 and prints each name with its type:import requests import urllib3 urllib3.disable_warnings() url = "https://192.168.1.1/restconf/data/ietf-interfaces:interfaces" headers = {"Accept": "application/yang-data+json"} response = requests.get(url, headers=headers, auth=("admin", "cisco123"), verify=False) data = response.json() for iface in data["ietf-interfaces:interfaces"]["interface"]: print(iface["name"], iface["type"])Output:
GigabitEthernet0/1 iana-if-type:ethernetCsmacd GigabitEthernet0/2 iana-if-type:ethernetCsmacd Loopback0 iana-if-type:softwareLoopbackTrace what happens after the response arrives:
response.json(): Converts the raw JSON body into a nested Python dictionary.data["ietf-interfaces:interfaces"]: Navigates one level deep into the container.["interface"]: Returns the list of interface dictionaries.forloop: Walks each dictionary and prints itsnameandtypekeys.
Exam questions often show this script and ask: "What is achieved by this script?"
Your job is to trace the keys until you reach the printed value.Answer the question below
In Script 2, what variable holds the parsed JSON dict from R1?
Answer the question below
In Script 2, what method is called on the response to convert it into a dict?
Answer the question below
In the for loop, what is the first key of the iface dict that is printed?
Script 3 — Handling Errors with try / except
Production scripts wrap risky calls in a
try / exceptblock.
If R1 is unreachable, the script prints a clear message instead of crashing:try: response = requests.get(url, headers=headers, auth=("admin", "cisco123"), verify=False) data = response.json() except requests.exceptions.RequestException as error: print("Could not reach R1:", error)Read the structure:
try: Runs the risky network call.except: Runs only if Python raises an exception insidetry.as error: Captures the exception message so you can print it.
On the exam, recognize the pattern:
tryandexceptalways come together.Answer the question below
Which keyword starts the block that runs only when an error occurs?
Answer the question below
Which Python keyword is used to give a name to the captured exception?
Reading data from a router is half the job.
The other half is changing its configuration using the same Python library with a different HTTP method.Script 4 — Updating Configuration with PATCH
The script below writes a new description on
GigabitEthernet0/1usingPATCH:import requests import urllib3 urllib3.disable_warnings() url = "https://192.168.1.1/restconf/data/Cisco-IOS-XE-native:native" headers = { "Content-Type": "application/yang-data+json", "Accept": "application/yang-data+json" } payload = { "native": { "interface": { "GigabitEthernet": [ { "name": "0/1", "description": "Configured via Python" } ] } } } response = requests.patch(url, headers=headers, auth=("admin", "cisco123"), json=payload, verify=False) print(response.status_code)Output:
204A
204 No Contentstatus means the change was applied successfully with no response body to return.Notice two key exam patterns in this script:
Content-TypeHeader: Required because the script sends a payload body.json=payloadParameter: Automatically converts the Python dictionary into a JSON formatted string for the HTTPS body.
Log into R1 and verify the change with the CLI:
R1# show run interface GigabitEthernet0/1 Building configuration... Current configuration : 98 bytes ! interface GigabitEthernet0/1 description Configured via Python ip address 192.168.1.1 255.255.255.0 endThe description set by Python appears directly in the running configuration without typing a single CLI command.
Summary of Python Automation Basics
You can now read and interpret any Python script in an exam exhibit. You recognize variables, lists, dictionaries, nested access, JSON parsing, loops, HTTP methods, headers, status codes, and exception handling.
Next comes YANG, the data modeling language that defines what those long RESTCONF URLs and JSON payloads actually point to.
Answer the question below
What HTTP status code did the device return when the PATCH succeeded?
Answer the question below
What HTTP method is sent to the router by the PATCH script? (one word, lowercase)
Answer the question below
On which interface did the PATCH script change the description?
Answer the question below
Which HTTP method would you use to fully replace the interface configuration instead of just updating part of it?
Answer the question below
If R1 returned status code 401 instead of 204, what would that mean?