You are on page 1of 26

DEVNET-3612

Coding 2002 - Useful Python


Libraries, Frameworks, and
Features to Master

Matthew DeNapoli – DevNet Developer Evangelist


Cisco Spark
Questions?
Use Cisco Spark to communicate
with the speaker after the session

How
1. Find this session in the Cisco Live Mobile App
2. Click “Join the Discussion”
3. Install Spark or go directly to the space
4. Enter messages/questions in the space

cs.co/ciscolivebot#DEVNET-3612

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
Agenda

• Introduction
• HTTP Calls with Requests
• YANG Model Data with NETCONF and ncclient
• Logging and Error Handling
• Web Frameworks (Flask)
• Conclusion
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

DEVNET-3612 © 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 5
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

DEVNET-3612 © 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 7
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

DEVNET-3612 © 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 9
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

DEVNET-3612 © 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 11
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!")

DEVNET-3612 © 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 13
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)

• WARNING # Log some messages


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.')

• CRITICAL Result:
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

https://github.com/ktbyers/netmiko

DEVNET-3612 © 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 14
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!")

DEVNET-3612 © 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 15
Python Web Frameworks
Some Python Web Frameworks

DEVNET-3612 © 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 17
What do they do for us?

• URL routing
• HTML, XML, JSON, and other output format
templating
• Database manipulation
• Security against Cross-site request forgery (CSRF)
and other attacks
• Session storage and retrieval

https://www.fullstackpython.com/web-frameworks.html
DEVNET-3612 © 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 18
A Flask example – Key elements

Website ”routes” defined using decorator


@app.route('/go', methods=['GET'])
def get_go():
return render_template('index.html',**locals())
HTML served from “/templates” and
resources from “/static”

DEVNET-3612 © 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 19
Demo
Cisco Spark
Questions?
Use Cisco Spark to communicate
with the speaker after the session

How
1. Find this session in the Cisco Live Mobile App
2. Click “Join the Discussion”
3. Install Spark or go directly to the space
4. Enter messages/questions in the space

cs.co/ciscolivebot#DEVNET-3612

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
• Please complete your Online Complete Your Online
Session Evaluations after each
session
Session Evaluation
• Complete 4 Session Evaluations
& the Overall Conference
Evaluation (available from
Thursday) to receive your Cisco
Live T-shirt
• All surveys can be completed via
the Cisco Live Mobile App or the
Communication Stations
Don’t forget: Cisco Live sessions will be available
for viewing on-demand after the event at
www.ciscolive.com/global/on-demand-library/.

© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
Continue Your Education
• DevNet: https://developer.cisco.com
• DevNet Learning Labs: https://learninglabs.cisco.com

DEVNET-3612 © 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 23
Got more questions? Come find me!
mdenaol@cisco.com
@denapom11
http://github.com/denapom11

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

DEVNET-3612 © 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 24
Thank you

You might also like