#CLMel Coding 1003 – Useful Python Libraries, Frameworks, and Features to Master

Kareem Iskander– Developer Advocate DevNet @Kareem_Isk DEVNET-2895 Cisco Webex Teams

Questions? Use Cisco Webex Teams (formerly Cisco Spark) to chat with the speaker after the session How 1 Open the Cisco Events Mobile App 2 Find your desired session in the “Session Scheduler” 3 Click “Join the Discussion” 4 Install Webex Teams or go directly to the team 5 Enter messages/questions in the team space cs.co/ciscolivebot#DEVNET-2895

© 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 3 Agenda • Introduction

• Manipulating XML in Python

• Working JSON in Python

• Dealing with YAML in Python

• Parsing CSV in Python

• HTTP Calls with Requests

• YANG Model Data with NETCONF and ncclient

• Conclusion

DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 4 Manipulating Data of All Formats

• XML - xmltodict • CSV

• pip install xmltodict • import csv import xmltodict

• JSON

• import

• YAML - PyYAML

• pip install PyYAML import yaml

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 5 Demystify XML XML- eXtensible .

GigabitEthernet2 A human readable data structure that Wide Area Network applications use to store, transfer, and read data. true

172.16.0.2

255.255.255.0

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 7 XML

• Designed for the Internet • Schema or namespace defines data model GigabitEthernet2 surround elements Wide Area Network for structure and layout true • Key/Value representation value

172.16.0.2 • Whitespace not significant 255.255.255.0

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 8 XML Object

• A related set of data surrounded by ?> •An object can contain other GigabitEthernet2 objects or data entries Wide Area Network value true contained within the object

tags 172.16.0.2 255.255.255.0

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 9 XML List

. List of data . Can be composed of XML objects 172.16.0.2 . Repeated instances of 255.255.255.0 for each element 172.16.0.3 255.255.255.0 172.16.0.4 255.255.255.0

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 10 Treat XML like Python Dictionaries with xmltodict

• Easily work with XML data

• Convert from XML -> Dict* and back • xmltodict.parse(xml_data) • xmltodict.unparse(dict)

• Python includes a native Markup (/xml) interfaces as well • More powerful, but also more complex * Technically to an OrderedDict

https://pypi.python.org/pypi/xmltodict netprog_basics/programming_fundamentals/python_part_3/xml_example.xml

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 11 Breakdown JSON JSON - JavaScript Object Notation

{ "ietf-interfaces:interface": { "name": "GigabitEthernet2", "description": "Wide Area Network", A human readable data "enabled": true, "ietf-ip:ipv4": { structure that applications "address": [ { use to store, transfer, and read "ip": "172.16.0.2", data. "netmask": "255.255.255.0" } ] } } }

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 13 JSON

{ • A data-interchange text format "ietf-interfaces:interface": { "name": "GigabitEthernet2", • Notated with {} for objects, [] "description": "Wide Area for arrays Network", "enabled": true, • Key/Value representation "ietf-ip:ipv4": { "address": [ • "key": value { "ip": "172.16.0.2", • Whitespace not significant "netmask": "255.255.255.0" } ] } } }

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 14 JSON Object

{ • Data surrounded by { } "ietf-interfaces:interface": { "name": "GigabitEthernet2", • An object can contain other "description": "Wide Area objects or data entries Network", "enabled": true, • Key/Value set separated by "ietf-ip:ipv4": { "address": [ { • No comma at the end! "ip": "172.16.0.2", "netmask": "255.255.255.0" } ] } } }

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 15 JSON List

{ . List of data "addresses": [ . Can be composed of JSON { "ip": "172.16.0.2", objects "netmask": "255.255.255.0" }, . Notated with { "ip": "172.16.0.3", . Comma Separated "netmask": "255.255.255.0" }, { "ip": "172.16.0.4", "netmask": "255.255.255.0" } ] }

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 16 To JSON and back again with json

• JSON and Python go together like peanut butter and jelly

• json.loads(json_data)

• json.dumps(object)

• JSON Objects convert to Dictionaries

• JSON Arrays convert to Lists

https://docs.python.org/3/library/json.html netprog_basics/programming_fundamentals/python_part_3/json_example.json #CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 17 Simplify YAML YAML - “YAML Ain’t Markup Language”

--- ietf-interfaces:interface: name: GigabitEthernet2 description: Wide Area Network enabled: true A human readable data ietf-ip:ipv4: address: structure that applications - ip: 172.16.0.2 netmask: 255.255.255.0 use to store, transfer, and read data.

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 19 YAML

--- • Minimalist format commonly ietf-interfaces:interface: used for configuration files name: GigabitEthernet2 description: Wide Area Network • Whitespace indentation defines enabled: true structure ietf-ip:ipv4: address: • No - ip: 172.16.0.2 netmask: 255.255.255.0 • Key/Value representation

• key: value

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 20 YAML Object

--- • Related set of data at the ietf-interfaces:interface: common indentation level name: GigabitEthernet2 under name description: Wide Area Network enabled: true ietf-ip:ipv4: • An object can contain other address: objects or data entries - ip: 172.16.0.2 netmask: 255.255.255.0 • key: value pairs left aligned

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 21 YAML List

--- . List of data addresses: . Can be composed of YAML - ip: 172.16.0.2 netmask: 255.255.255.0 objects - ip: 172.16.0.3 netmask: 255.255.255.0 . Uses “-” character to indicate - ip: 172.16.0.4 a list element netmask: 255.255.255.0

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 22 YAML? Yep, Python Can Do That Too!

• Easily convert a YAML file to a Python Object

• yaml.load(yaml_data)

• yaml.dump(object)

• YAML Objects become Dictionaries

• YAML Lists become Lists

https://pypi.python.org/pypi/PyYAML/3.12 netprog_basics/programming_fundamentals/python_part_3/yaml_example.yaml

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 23 CSV, just for fun Import Spreadsheets and Data with csv

• Treat CSV data as lists • csv.reader(file_object)

• Efficiently processes large files without memory issues

• Options for header rows and different formats

https://docs.python.org/3/library/csv.html netprog_basics/programming_fundamentals/python_part_3/csv_example.csv

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 25 Access Different Easily • REST APIs – requests • pip install requests import requests

• NETCONF – ncclient • pip install ncclient import ncclient

• Network CLI – netmiko • pip install netmiko import netmiko

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 26 HTTP Calls Make HTTP Calls with Ease using requests

• Full HTTP Client

• Simplifies authentication, headers, and response tracking

• Great for REST API calls, or any HTTP request

http://docs.python-requests.org

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 28 YANG Model Data YANG Model Data with NETCONF and ncclient

• Full NETCONF Manager (ie client) implementation in Python

• See later presentation on NETCONF details

• Handles all details including authentication, RPC, and operations

• Deals in raw XML

https://ncclient.readthedocs.io netprog_basics/programming_fundamentals/python_part_3/api_ncclient_example.py

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 30 CLI access For When CLI is the Only Option – netmiko

• If no other API is available…

• Builds on paramiko library for SSH connectivity

• Support for a range of vendors network devices and operating systems

• Send and receive clear text

• Post processing of data will be key https://github.com/ktbyers/netmiko

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 32 Error Handling and Logging Error Handling – Try/Except device_list=[] try: resp = requests.get(post_url,headers=headers,params="",\ verify = False) response_json = resp.json() print ("Status: ",resp.status_code) except: print ("Something wrong with GET /host request!")

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 34 Python Logging # Import the logging module import logging • DEBUG # Specify to log to a file, specify the format for the message and the date format and the logging level logging.basicConfig(filename='mylog.log',format='%(asctime)s %(levelname)s: • INFO %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG)

# Log some messages • WARNING logging.debug('This is a debug message. You should see this in the file.') logging.info('This is an info message. You should see this in the file.') • ERROR logging.warning('This is a warning message. You should see this in the file.')

Result: • CRITICAL 01/10/2018 01:14:15 PM DEBUG: This is a debug message. You should see this in the file. 01/10/2018 01:14:15 PM INFO: This is an info message. You should see this in the file. • Push to Console 01/10/2018 01:14:15 PM WARNING: This is a warning message. You should see this in the file.

• Push to File

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 35 Error Handling and Logging Together

... try: resp = requests.get(post_url,headers=headers,params="",verify = False) response_json = resp.json() logging.info("Status: ",resp.status_code) except: logging.error("Something wrong with GET /host request!")

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 36 Got more questions? Come find me!

[email protected] @kareem_isk http://github.com/kiskander

@CiscoDevNet facebook.com/ciscodevnet/ http://github.com/CiscoDevNet

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 37 Q & A

#CLMEL DevNet Code Exchange

Build your developer cred Share your code

Get your code in front of the DevNet Community developer.cisco.com/codeexchange

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 39 Continue Meet Labs Cisco Related your the Demos Sessions education Expert in WOS

#CLMel DEVNET-2895 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 40 Complete Your Online Session Evaluation • Give us your feedback and receive a complimentary Cisco Live 2019 Power Bank after completing the overall event evaluation and 5 session evaluations. • All evaluations can be completed via the Cisco Live Melbourne Mobile App. • Don’t forget: Cisco Live sessions will be available for viewing on demand after the event via https://ciscolive.cisco.com/

#CLMEL © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 41 Thank you

#CLMel #CLMel