So it's time for us to actually look at a Python script
and see what is it that this Python script does.
Here's the spoiler alert.
I'll tell you right now what this script does
is it connects to a FirePOWER Management Center REST API,
and it retrieves a list of applications
that FirePOWER Management Center is monitoring.
It is a pretty basic script.
There are only a couple interactions
that take place in this script.
But still, if you are brand new to Python and API,
this could be a little overwhelming. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
this could be a little overwhelming.
So what we're going to do is, we're
going to pull out the sample code that I've created,
and we're going to go through it one line at a time
so that we really understand what's going on.
In the first Nugget that we're going to do,
right now, we're going to just set up
the basics of how the Python script works
by importing what's called libraries
as well as setting up variables.
This is a couple of major topics that make Python really cool,
so let's get going.
So this is the code that we're going to be working with.
If we scroll down here for just a second, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
If we scroll down here for just a second,
you can see there's a lot of stuff that's going on
right now, and that's OK.
It looks a little complicated and overwhelming,
but don't worry.
The entire point of this skill is
so that we can read it and understand what it is
that it's really trying to do.
Now, as we go through this, I'm going
to show you where you can actually access this code
and what is it trying to do in the first place.
What is it trying to target.
Let's set that up right now.
You may be looking on your screen right here,
and you see this URL.
This is the FirePOWER Management Center REST API sandbox. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This is the FirePOWER Management Center REST API sandbox.
This exists in the DevNet sandbox.
You can get there by going to devnetsandbox.cisco.com,
and you can reserve a private instance of the FirePOWER
Management Center.
All you would then have to do is change the user name
and password later when we get to that point.
Now, don't worry if any of this stuff
seems like gobbledygook right now.
We're actually going to tackle this
in the upcoming few minutes as we jump into what
this code is actually doing. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
this code is actually doing.
But I just wanted to let you know that you can actually
follow along if you have Python installed
and you have all of the correct libraries installed.
If you don't have Python installed
and you don't have the libraries installed,
we do have content like at CBT Nuggets on DevNet Associate,
where we walk through how to set these items up.
Beyond that, you can follow along with this exact code
by pulling this down from my GitHub repository.
This is publicly accessible code,
and the link is popping up on the screen
so that you can just browse through this website WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
so that you can just browse through this website
and at least look at it, follow along, copy and paste it
into your own VS Code environment
or whatever iEditor you want to use,
and you can follow along that way.
Let's start interpreting this script.
Like we already said, the purpose
of this script, which is called fmc get apps.py
is to give a list of applications
that FirePOWER Management Center is monitoring.
Now, what we're going to focus on real quick
is lines 1 through lines 11.
That's this section right here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
That's this section right here.
Don't worry about any of this stuff that's
coming up after the fact.
We're going to tackle the script a little bit at a time.
And the first thing you're immediately
seeing at the top of the list are these Import statements.
What's that all about?
You see, Python out of the box, it
can do a lot of amazing stuff.
You just install Python, and it can do a lot of stuff,
but it can't do everything.
What we have to do is, we have to add what's
called libraries into Python.
These are basically like plug-ins
that extend the capability of Python. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that extend the capability of Python.
That's why they call Python an extensible languages.
It's because we can add functionality to it.
So when we see import json, this is
taking our default installation of Python
and adding the ability to work with JSON data
in this particular script.
When we see import sys, this is taking our default Python
functionality and adding the ability WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
functionality and adding the ability
to work with the actual system, the actual operating
system itself.
And when we import requests here, what request does
is this takes the default Python functionality and adds
the ability to perform HTTP requests.
Recall for a moment that a REST API, the entire point
of the REST API is so that we can work and leverage
all of the functionality of the HTTP protocol to gather data.
Remember, way back when we were talking about how you browse WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Remember, way back when we were talking about how you browse
to a website, and what a web browser is doing
is downloading HTML documents and CSS documents
and JavaScript documents and turning them
into a pretty website.
Whereas, an application will also
use HTTP, but instead of downloading
HTML documents, what it does is it downloads things
like JSON documents.
Here, you've now seen how Python adds the ability
to work with HTTP and adds the ability to work with JSON data. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to work with HTTP and adds the ability to work with JSON data.
These import statements really only exist
for this specific script.
If I create a new script that also needs to work with HTTP
and also needs to work with JSON,
I've got to specify these import statements again.
So the first thing you should know
when interpreting a basic Python script
is you should look at these import statements and think,
what functionality am I adding to Python
for this particular script?
JSON is going to be a big one.
Request is going to be a big one. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Request is going to be a big one.
Some other ones that are worth pointing out just
for the sake of it is ncclient.
This is so that you can write a script that works
with the NETCONF protocol.
There is also lxml.
This is so that you can work with xml data.
If you are an overachiever and you really
want to explore some fun stuff that Python can do,
you can use a library like openpyxl.
This is for working with Excel files
so that you can actually import data from an Excel file. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
so that you can actually import data from an Excel file.
This is a really popular thing for network engineers
because they may have an Excel file that
contains all of their network devices, IP addresses,
usernames, and passwords.
So they can import the Excel file
and then log into each one of those devices
and perform some sort of action.
It's a really, really cool thing to do.
Lastly, you may want to check out a library like selenium.
Selenium is used to emulate a web browser like Firefox.
So you can have Python launch a web browser, go to some page, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So you can have Python launch a web browser, go to some page,
parse out the information using something like selenium,
and they would all show up here on the import statements.
The key thing that we're trying to set up with
is if you just install Python and you don't import something
like openpyxl, you can't work with Excel files.
But as soon as you put the import statements there
and you've installed openpyxl, there you go.
You can now work with Excel files within Python,
and that's extending Python's functionality.
So in this particular script, we now WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So in this particular script, we now
see that we're importing the ability to work with JSON data.
We're importing the ability to work with HTTP endpoints,
and we're importing the ability to perform
operating system-style tasks using those sys libraries.
Now, what we need to talk about is the actual variables
themselves.
This is a crucial part to working with Python.
Let's talk about that.
So the remaining lines that we have here, or we can see I've
got a little comment with this little pound symbol here WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
got a little comment with this little pound symbol here
that says, set up variables.
Whenever you see a pound symbol followed by any text,
that's just a comment in the line of code
that Python absolutely ignores.
That way, anybody who's following along or reading
your scripts after the fact can see
what's happening in the code underneath because you've
written a little comment here.
So let's focus here on line 6 and line 7.
I have this thing called URL, and then I see a URL.
What I can do in Python is called creating variables. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
What I can do in Python is called creating variables.
Rather than typing this whole URL out multiple times
throughout the code, I can just store it
in a little object called URL.
So this we can see is the base URL.
It's fmcrestapisandbox.cisco.com,
and then the endpoint is the login your URL.
This is what gets tacked on to the end of it.
The key thing here is that I can have a base URL called URL,
and then I can change the endpoint WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and then I can change the endpoint
by specifying a different endpoint after the fact.
If we skip ahead, you can see exactly where I've done this.
You can see I've got your URL, and then we
concatenate login URL right to the end of it.
The beautiful thing about this is
I don't have to type all of this long text
over and over and over again throughout my code.
I can just specify it by using URL or login your URL.
And in the next Nugget, we're going
to talk about getting logged in to the API.
That's actually what we're setting up here, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
That's actually what we're setting up here,
and that's what we're doing first.
We're trying to set up how we get logged in to the API.
You see, an API is no different from any other secure website
where you have to log in before you can do stuff.
Wouldn't it be terrible if our FirePOWER Management Center
device was open to the public and anybody could get data out
of it?
That would be bad.
That's why we have to secure our API by getting logged in.
So to help me build this request so that I get logged in,
I've created two little variables here, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I've created two little variables here,
one that specifies the base URL that I can reuse over
and over again and then the specific login URL that says,
we're going to be logging in and generating a token.
Now, I'm not going to jump ahead too
much because the next Nugget is all about getting logged in
and handling that login request.
But just know that for now, what we're doing is
we're setting up variables that hold the URL as well
as a couple other things.
Headers are another very important part
to the HTTP protocol.
The headers tell our endpoint that we're WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
The headers tell our endpoint that we're
trying to connect to what kind of data
are we trying to send in, what kind of data are we
trying to retrieve.
When you see content type, this is
saying we are sending in content of JSON data.
And again, we're going to talk more
about that in the next Nugget when we're talking
about getting logged in.
This is, again, just setting up that we're
specifying our headers in a variable that specifies
all of this information.
Lastly, we have a username and a password. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Lastly, we have a username and a password.
Now, these are hard coded into the code itself,
and that's a big no-no in security world.
We've already talked about that when we talked
about how to secure our code.
This is just for demonstration purposes,
and this is, again, using something
in the DevNet sandbox.
If you are following along and you're reserving the FirePOWER
Management Center in DevNet sandbox,
you'll receive an email that has your user name
and a randomly generated password.
This password changes every time you reserve the DevNet sandbox. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This password changes every time you reserve the DevNet sandbox.
So to recap what's happening here,
we're focusing on just these first few lines of code,
and what we see here is that we've
got our import statements, which are adding the functionality
to work with HTTP, to work with JSON in this particular script.
Then we're setting up variables throughout the code.
That way we don't have to retype all of these things
over and over again.
I can just type the word URL.
I can just type the word headers,
and it refers back to all of this stuff
that we're storing in the variable. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that we're storing in the variable.
So that's been setting up the first batch of our code.
In the next Nugget, we're going to dive
deep into how we get logged in to this API.
I hope this has been informative for you,
and I'd like to thank you for viewing.