Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
175 views

Python Part 3 Useful Python Libraries For Network Engineers PDF

Uploaded by

mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views

Python Part 3 Useful Python Libraries For Network Engineers PDF

Uploaded by

mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Python Part 3: Useful Python

Libraries for Network Engineers


A Network Programmability Basics Presentation

Hank Preston, ccie 38336


Developer Evangelist
@hfpreston

Network Programmability Basics/Programming Fundamentals/Python Part 3: Useful Python Libraries for Network Engineers
Network Programmability Basics Modules
• Introduction: How to be a Network Engineer in a Programmable Age
• Programming Fundamentals
• Network Device APIs
• Network Controllers
• Application Hosting and the Network
• NetDevOps

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Network Programmability Basics: The Lessons
Module: Programming Fundamentals
• Data Formats: Understanding and using JSON, XML and YAML
• APIs are Everywhere... but what are they?
• REST APIs Part 1: HTTP is for more than Web Browsing
• REST APIs Part 2: Making REST API Calls with Postman
• Python Part 1: Python Language and Script Basics
• Python Part 2: Working with Libraries and Virtual Environments
• Python Part 3: Useful Python Libraries for Network Engineers
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Code and Develop Along
• Get the Code!
• github.com/CiscoDevNet/netprog_basics
• Setup Lab Prerequisites
• Each lab includes a README with details
• Access to Infrastructure
• DevNet Sandbox
• Specifics in lab README

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Topics to Cover
• Libraries to Work with Data
• XML
• JSON

• YAML

• CSV

• API Libraries
• REST APIs
• NETCONF/YANG

• CLI

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Libraries to Work with Data

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Manipulating Data of All Formats
• XML - xmltodict • CSV
• pip install xmltodict • import csv
import xmltodict
• JSON
• import json
• YAML - PyYAML
• pip install PyYAML
import yaml

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
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


(html/xml) interfaces as well
• More powerful, but also more
complex
* Technically to an OrderedDict

https://pypi.python.org/pypi/xmltodict
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

netprog_basics/programming_fundamentals/python_part_3/xml_example.xml
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
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

netprog_basics/programming_fundamentals/python_part_3/json_example.json
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
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

netprog_basics/programming_fundamentals/python_part_3/yaml_example.yaml
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
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

netprog_basics/programming_fundamentals/python_part_3/csv_example.csv
API Libraries

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Access Different APIs Easily
• REST APIs – requests
• pip install requests
import requests

• NETCONF – ncclient
• pip install ncclient
import ncclient

• Network CLI – netmiko


• pip install netmiko
import netmiko

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
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
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

netprog_basics/programming_fundamentals/python_part_3/api_requests_example.py
Demo Time!

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
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
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

netprog_basics/programming_fundamentals/python_part_3/api_ncclient_example.py
Demo Time!

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
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
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

netprog_basics/programming_fundamentals/python_part_3/api_netmiko_example.py
Demo Time!

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Summing up

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Review
• Looked at how to use Python libraries to work with XML, JSON,
YAML and CSV data
• Learned about libraries for leveraging REST APIs, NETCONF, and CLI
interfaces

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Call to Action!
• Complete the full Network
Programmability Basics Course
• Run the examples and
exercises yourself!
• Bonus Examples!
• Join DevNet for so much more!
• Learning Labs
• Development Sandboxes

• Code Samples and API Guides

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Got more questions? Come find me!
hapresto@cisco.com
@hfpreston
http://github.com/hpreston

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

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

You might also like