Python libraries and modules extend the capabilities
of Python into the subsystems, networking,
and all the other system functionality you need as a
security professional to monitor, test, and secure.
Let's start with a few of the standard modules built in and work our way into
some of the more complex third‑party libraries you will need.
The sys module is our first built‑in module we will walk through today.
Yes, we still have to import it into our script,
which we will show you in a second,
but the module does not need to be downloaded or installed with the pip first.
Details on the module can be found directly in the python.org site.
Each function, including some of those we have already talked through,
are broken down with some of the more important details on how to implement.
You do not get every final detail, so if there's anything intriguing to you,
I would suggest researching deeper to understand what
versions of Python the function works with and what all
the function is capable of doing.
Some come with multiple parameters and nesting
abilities to expand on other functions.
As mentioned,
this is part of the standard library and does not require you to download,
but does have to be imported.
So what is this?
Well, it is a set of functions that allow you to interact with the interpreter.
Taking a look at a few of these, I mean,
there are a few dozen,
so be sure to use the link to the official sys module documentation,
but some you might see in your work, argv,
which allows you to access any command line
arguments when kicking off the script.
These are used to turn static scripts dynamic without
having to hard code your changes.
Just note, these are not inputs requested while the script is running,
you will use the input function for that,
but rather these are command line arguments added to the start string.
An alternative to sys.argv is the library argparse.
Argparse helps to automatically parse all the command line
arguments and assign them to variables to be used.
But for this course,
I'm going to stick with the argv; it is more a straightforward approach.
Path is a list of everything loaded into the path
environment variable of the operating system.
This orders the various paths in a list with path0 being
the directory containing your script.
The stderr is part of a series of stdin, stdout, and stderr inputs and outputs.
Stderr is important because it will alert you when any part of your
script has an issue so that it can be addressed.
Version provides you with both the build of the interpreter used,
as well as some additional information on the operating system architecture.
You can use this to view, but I would suggest using the version_info,
which will break down the information into a list that you
can use to help you determine if the user is running a
supported build for the script.
All right, we're going to walk through a quick little demo of the sys module.
As we talked about, you simply have to import it to use the functions of sys.
One of the major functions that we kind of pulled out
here was talking about the stdin, stdout,
and stderr when we simply go through and print those
messages to the stdout or stderr,
you're going to see everything prints directly on the screen.
That's all fine and dandy, but you ask yourself,
well why, what's the difference then?
All right, so what we're going to do is we're going to take the stderr,
and this time we're going to print it to a log file.
As you can see here,
I'm printing it to a log file that sits within the same directory,
and I don't have that log file currently,
so when I run the script,
the only thing that prints on the screen now is my standard
output and all my errors have disappeared.
Why have my errors disappeared?
Well, as you see here, it's created that output error log file,
and when I open it, you see all the error messages are in there.
Going back, I could also comment out my stdout.
So now when I go to print, all my standard output and my standard errors,
let me delete my error file here,
my stdout and my stderr have all disappeared and now
you see I have an out‑err and an out, and inside the out I have all my stdouts,
and inside there I have all my stderrs.
So that's when we have kind of like splitting it up if you want
all your error messages to go into a log,
if you want all your error message to print on a screen and you want your
output to go to a log, there's a lot of different combinations you can
take to kind of split up where you want your output to go to based on the
error message, based on stdout.
So we're going to get rid of all this now. That was a quick little
demo of that. We're going to talk about the input.
So, a standard way of inputting is, hey, I'm going to ask you for something
and then you type that in and I'm going to do something with it. In this
case I'm just simply printing it on the screen.
So let's go to this demo now. And it's going to ask me, hey, what's your name?
I'm going to put in Mike. I'm just going to say Hello Mike.
But another way of kind of going through this is to say tell me who
you are, but enter that as part of calling the script. And so when
it says that argument we say Hello.
So now when I call the script,
what I can do is I can add a space with some arguments on the end,
one of them being my name, and when I hit Enter, it's going to
say Hello Mike. So it grabs that and prints it.
So looking at this uncommented, so I have two ways of inputting here.
I have entering it directly into the script and entering
it as part of the call to the script.
So when I enter it as part of the calls to the script,
I could put in John here, and when I enter it into the input of the script,
I say Mike. So I have Hello Mike because I grabbed it from my input
and I have Hello John because I grabbed it from my argument that I
added as part of calling the script.
One thing I also want to call it here is if you want to have a name, file,
whatever the case may be that you're entering here with a space in it,
you need to put quotes around that so that it simply calls the entire thing now.
So Hello John Smith. If I were to forget those quotes
and I hit Enter, let me type in Mike,
it's just going to say Hello John because it's going to treat those as two
separate arguments. So we're going to get rid of all that now.
We're going to look at the next item on the list here.
So we talked about the path,
we talked about the version, and we talked about version_info. So
these four n's are really two different ones.
The path talks about your system variables,
adding that 0 onto the end of it just pulls out your main working directory.
So what am I working off of for this script?
The path pulls out the full path from the system variables.
Version pulls out what version of Python you're working off of on one
line printed out. And version_info prints out all the different parts
into separate areas, so your major version,
your minor version, it will print all that out into separate parts on the screen.
So when I go to print this,
what you're going to see here is at the top I have my working
directory, so that's the path I'm working out of.
I have my full path, which is everything in my system variable.
I then have my version all laid out nicely here on one line. And then I have
version_info, which breaks up that version into your major,
your minor, your micro releases.
And what's nice about that is if you're writing a script that's
written for a very specific version of Python where it only works up
to a certain version of Python, it needs a certain version of Python
in order to work, you can take a look at this version_info as part of
your script and throw out an error message or throw out some type of
warning saying,
hey, this script may not work because you're using the wrong version of Python.