Time
10 hours 10 minutes
Difficulty
Intermediate
CEU/CPE
15

Video Description

The Journeyman String Server This lesson offers step by step instructions in running a command and then testing it in the Journey Man String Server to make sure it works. Participants can work along with the instructor on executing commands, seeing if they work, printing commands and also ways to fix issues if they fail. Participants learn about: Generators Using networking to create a client to obtain information Returning a socket object s.receive buffer size Python help function Classes

Video Transcription

00:04
Welcome back
00:05
in this video, we're gonna be going over the journeymen activities, the journeyman Python activities.
00:09
We're going to go through the four Byner Challenges that we have. And then the next video, we'll be covering the actual capstone.
00:17
So we're just gonna dive right in the 1st 1 Journeyman One
00:21
is take two arguments. A list in an integer.
00:25
The list is a series of strings and one of those strings will be the file name
00:29
the others. The other strings will be the file contents.
00:32
The imager, which we're gonna call item is the location in the list of the string that we're going to use is the vile name.
00:39
Uh
00:40
So, for example, if we get a string list of, you know, just
00:44
we'll do it.
00:45
If we get a string list that looks like a B C D E f.
00:51
And we got a manager that is
00:54
44 not five.
00:57
Then we're going to call a zero. Of course, be is one
01:00
two
01:03
his three cheese four. And if it's five,
01:07
so this
01:10
I'll be the file name
01:12
and
01:14
these will all be the contents.
01:22
It's a little complicated. Thio sort out in just the text, so it'll be a little bit easier once we actually
01:27
dig in and start doing it.
01:30
List is here.
01:33
Item is here.
01:34
So
01:37
first thing we've got to do, obviously, is we've actually got to
01:41
identify the string or identify the file name. So we're gonna do file name
01:49
equals stir
01:51
start list
01:53
item.
01:53
The reason we're doing this is just in case we get something weird. And they passed this an image or something like that. We want to make sure it's a stir so that it
02:00
creates a proper file name.
02:04
So we've got a final name.
02:05
Pretty sure it 14. Easy. Then we're gonna actually create the file.
02:07
F equals open.
02:10
We're an open file.
02:12
Ah, File name.
02:15
And then f open open. Takes another argument, and we're gonna open up our
02:20
by then interpreter to see what the argument is. So python
02:24
ready to help
02:25
open. I remember
02:28
parentheses here.
02:30
Just the file object.
02:31
Okay. It takes a few possible arguments
02:36
open name, and then it needs mode or buffering.
02:39
Uh,
02:40
see file dot doc for further information.
02:44
So this doesn't really tell us that much, so let's see what filed out. Doc tells us
02:50
file dots underscore. Underscored toc Underscore. Underscore.
02:54
Okay, there's some stuff. So Moken b r w or a
02:58
um ours read w is right. A is upend.
03:04
Ah, you can add a plus to the moto last simultaneous reading and writing.
03:07
That's what we're gonna want to do
03:08
on the buffering thing we're not going to worry about.
03:13
That's just not really something necessary.
03:16
So
03:20
exit here,
03:22
command prompt.
03:23
So we're gonna open this with W Plus
03:28
all that means we're going to open it for writing or reading
03:30
nice and easy.
03:34
Then we're going to say,
03:35
for I
03:37
in stir list
03:44
If I
03:46
it's not equal
03:46
to stir list
03:50
item
03:51
is again.
03:53
We're already using that as a file name
03:54
f dot Right.
03:58
See, It's got a bunch of different things that need to the file name, and then you have the option to give it a compression
04:03
or other arguments. But we just need
04:06
what we're actually writing.
04:09
F not, right.
04:12
Uh, I
04:15
and that's pretty much the end of it.
04:17
After that, we're going to do f dot clothes. So we close the file always, always, always remember to close your files, and then we're gonna test this.
04:27
So we're ready to test. We're gonna pop over to the journeyman activities tester dot pie. This is, of course, like how the others included
04:33
in the materials to the side.
04:36
So we're gonna open this and we're gonna run it.
04:41
It says success. So it worked.
04:44
Uh, you don't really need to see the code in this, And it's a little
04:46
crazy and nuts. Some listed orations and generations of lists within lists. But
04:54
long story short, what it's doing is it is opening the file.
04:57
It's checking East line in the file to make sure it's doing what it thinks it's doing.
05:01
We're streaking. You joining in the file,
05:02
the to make sure it pretty greatly. And then it
05:05
prints it
05:06
if it failed.
05:09
So we'll go ahead and I'm gonna drop a print line in here.
05:13
A print in here. You already have it in yours.
05:15
Um, actually print
05:17
the actual line that it gets for this
05:20
information that it gets from the file to show that it is actually running everything correctly
05:26
and that
05:28
just
05:30
clean it up a little bit,
05:34
print a new line,
05:39
so it's easier to read
05:42
dealing with the wrong direction.
05:45
There we go. So you see, he gets a C D. This is a test
05:48
Adam. 234 and five.
05:51
That's what we wanted. And it tells us success.
05:54
Awesome. Finished the first activity. The recital.
05:59
All right, second item 1.2.
06:01
Ready Function, which takes a single imagers an argument and returns to some of every integer up to and including that number.
06:08
Use the generator.
06:10
This is a little trickier. It's not too bad, but it's a little trickier.
06:14
So for this one,
06:15
we're getting in the final number in the set
06:18
so
06:19
we could use if we were using the built in generator.
06:24
Be relatively easy to do, actually be some range
06:28
final number.
06:38
So just does it
06:39
clarification and show that that is what we would want to do. Well, actually,
06:43
because rain just on inclusive, we want to do that. But just to show that that's kind of how it works is we can go over here and we can pass
06:50
Well, we have a minus. Sign my return over here.
06:53
Fantastic recon. See tennis. 55 Forest 10. 1000 is 505 100
06:59
and 36 is
07:00
666 Well, that's unfortunate number.
07:03
Didn't work. Didn't expect that. I'll change out to 35 just for the more
07:10
peacefully minded in the audience anyway.
07:13
So, yes, this is
07:15
what works. And as you can see in my tester,
07:16
that's how I'm testing it to make sure you're doing it right.
07:19
But we're actually going to implement
07:23
the range
07:25
built in.
07:27
So instead, we're going to
07:30
first, we're gonna comment this line out. Leave it is the objective.
07:33
So we're gonna do a return,
07:35
and it will be
07:39
range some.
07:41
So we've got to get range that we've got to figure out how to make this happen.
07:45
So
07:47
as you can pretty obviously guess we're going to be using a generator.
07:50
A generator starts out as a while. Loop every generator starts is a wild loop with hope
07:58
through do well,
08:00
Current numb
08:01
is less than final them.
08:07
And because we actually wanted to be inclusive with me. While it's less than or equal
08:13
final month,
08:16
we're gonna say current nam
08:18
starts zero.
08:22
So while it's less than or equal to final them
08:24
current numb
08:28
plus equals one,
08:33
it's going to increase.
08:35
But first, what we want to do is, they actually want to do what's called a yield
08:39
covered yield in the slides. But
08:43
just for a quick re summary yield is going to output something without actually
08:46
doing a return or an end.
08:48
We're going to yield
08:50
current numb.
08:54
But here's the thing
08:54
is that you can't yield
08:56
without some sort of function. You've got to treat a generator, has to pretend to be a function.
09:01
So we're actually not going to be able to do this one
09:03
all in one function. We're gonna have to create our own,
09:09
so we're going to call it death
09:11
some generator.
09:16
And this is what we call an internal function. This isn't something that's referenced overhearing our tester. It doesn't need to know about it,
09:22
but it's an internal function. It's something that is necessary for your,
09:26
um,
09:26
public functions to use properly. There's actually an entire process to creating internal functions, but we're not really gonna worry about that right now,
09:35
So this is also going to get the final numb.
09:41
We're gonna drop our code in here.
09:45
Look.
09:46
Yeah,
09:48
So now what we're gonna do very easily is we're going to some
09:54
some generator
09:58
final numbers. The argument
10:01
some generator is actually going to return,
10:05
it's going to yield over time. It's going to give us each number in that list, and some is going to calculate all of those
10:11
a t end of which will be able to well range
10:16
some people. Is that
10:18
so? The end. We're just gonna return
10:20
the result of some going through this generator.
10:24
So let's have a crack at it and see if Python likes what we've done.
10:28
Door activities Tester
10:33
And look at that
10:33
test was a success.
10:35
It's really,
10:37
as you can see, really straightforward.
10:39
Uh, the trick to generators Is this right here? The yield?
10:43
All it's doing is it's returning a value without ending execution.
10:48
What were we actually just implemented
10:50
is something that I tend to write pretty often
10:52
because I am very. It's very easy for me to get that to forget, to put in this plus one,
10:58
and it's easier, and I actually find that I save time by quickly writing this function ahead of time. Just as a range plus one,
11:07
uh, is the equivalent of a range of a number of plus one, rather
11:11
so with that, you finished the seconds minor activity.
11:16
Pretty painless. Pretty quick. Pretty easy.
11:18
And we're gonna move on to the third.
11:20
Uh, this one is going to be our first adventure with networking.
11:24
Ah, you're gonna write a Python script which connects to the included server
11:28
on Port 5 50,000 and one and returns the message it receives.
11:33
So it's gonna be pretty
11:37
pretty hefty.
11:41
We have, ah, server, the journeyman string server over here,
11:45
and it's really basic service. Had a message list that it's gonna
11:48
open. It's gonna send one message each time you connect to it
11:54
and only commit through them.
11:54
So your job is to actually
11:58
do, uh, create this client that's going to go out and get that information.
12:03
First thing we've got to do if we're gonna be doing this is import Socket
12:05
socket is a really, really important module. It's the basis of
12:09
all networking,
12:11
everything you ever going to do across the network, your
12:13
going to need socket for it,
12:16
and for that reason,
12:18
you're gonna want to learn about it. So I recommend you take the time. We're not gonna do it with just the help in the dirt, because there's a lot in it.
12:26
We're going to go over some of the things with
12:30
the help in the dirt, but we're going to kind of gloss over because there is a lot of information, but it is one that you should absolutely go and look up and really do. Some reading on sockets are very important.
12:41
So we do import socket,
12:43
and then we're gonna do
12:45
your sockets. We're gonna see what options are available, what functions are available.
12:50
And there are a lot
12:52
that we can kind of
12:54
pick one out of that. For example, we're gonna do
12:58
help,
12:58
socket, not sucking.
13:01
That's the one that we're gonna be using.
13:03
All right,
13:03
so we see.
13:09
Ah, a bunch of other methods of socket. Are you there? Socket objects?
13:16
But what socket? Not sock. It actually does. Which I think it may have said at the top, but there's a lot of operation. What? Socket down stock it actually does. Is it returned the socket object?
13:26
Um, of a type that we define.
13:28
So
13:30
we're gonna look at this. We're gonna say Okay,
13:33
sockets well s, which is gonna be what we call her socket.
13:37
It will suck it
13:39
dot socket.
13:41
Now we're gonna need to arguments here.
13:45
The argument that we're going to be using for the purpose of this is a fine et
13:48
and
13:50
sock
13:52
stream.
13:54
And we, of course, we covered both of these. If you haven't gone, if you haven't been actually watching the slides videos, we cover both of these. We cover a lot of socket information in those, so it's worth pausing this to go check that out.
14:07
But remember, since we imported this module instead of importing everything in it,
14:09
we need to specify where these air filed, which means socket, not a finite and socket dot socks street.
14:16
So now we have a socket.
14:18
Uh,
14:18
all that's really doing is it's opening up
14:22
and saying, OK,
14:24
this socket exists here is an object for it s
14:28
as is now a socket object
14:31
with
14:33
ivy before and T c p
14:37
Which is what?
14:39
Yeah, fine. It is the i p for suction misty speak.
14:41
So then, after we create a socket,
14:45
we need to
14:46
connect.
14:48
Uh, so the host is going to your local host
14:52
so we can just give it a local host and the address is going to be
14:54
five or 50,000 and one
15:03
test Arctic set for it, not accept but s dot
15:09
Receive. That's we're looking for
15:15
which in Python It's actually not the word. Receive its r e c b. Sorry. There we go.
15:20
We're gonna do received string
15:24
equals as stock receive.
15:26
They were going to us close
15:30
and we're gonna return overseas string.
15:33
So this is obviously super basic, but it connects. It creates a socket.
15:39
It goes somewhere too.
15:41
Connect to the server. In this case, we're using these quotes here represent that it's the local machine and you're using every interface you can get your hands off.
15:50
Then we receive a string
15:54
which is get whatever this under the service sending
15:56
what server sends
15:58
and then we close the socket closing stock. It is important
16:03
because if you don't close it than it might send more information or line might go, You may be using resource is that need to be released?
16:11
Closed.
16:14
So
16:15
let's
16:17
first of all, we're going to start up the journeyman string server,
16:19
Move that over here. Just kind of hide that
16:22
a bit.
16:23
We're gonna run our activities. Tester.
16:26
We're going to see if it works.
16:30
No,
16:30
that would exploded.
16:41
Okay, so it's not gonna let us connect with quotes in this one. You could do that in a, sir, but you can't do that in your local machine.
16:47
Uh, are you can't do that when connecting out to the server,
16:51
which is fine. We can just do
16:52
the string of the local host. I P address was just 127001
16:59
Now let's try.
17:00
So the reason, by the way to explain why you can't actually have just an m p i p field is that when you're connecting out, it's actually trying to go to the Internet.
17:07
So rather than connecting on every every resource you have available, you have to actually say who's resource is you're reaching.
17:15
So there you go
17:15
that we're gonna
17:18
we've still got our server up, as you can see. So we're gonna go ahead and run this
17:22
and it's still going to explode.
17:25
But this is a different one. So now we c. O s not received takes an argument. Okay,
17:30
Well,
17:32
important thing first of all, is that we have a new and special error. But let's see, what
17:37
are you received? Takes
17:38
you open a python?
17:41
If I was smart I would leave this open, but, you know,
17:45
import socket and we're going to help socket not receive E.
17:53
That's weird.
18:00
All right, so that was a bit of a problem. I had to look through the python, help and figure out what's going on.
18:06
So what it actually is, what we need to do is help socket, not socket,
18:11
not receive.
18:11
The reason for this is within. Is that the original sock? It actually doesn't have a function called Receive.
18:17
It's the socket, not socket function, which does,
18:21
Um, that's really complicated and
18:22
it yet, but
18:26
there you go. So if you want to get help on the socket, receive what you actually need to do is socket dot socket not received
18:36
and that we're actually going to call that like it's a function. There you go.
18:38
So
18:40
receive what? You Actually, it is a buffer size eyes the argument that it needs to have, and it's going to return the data.
18:47
After a little bit of trekking, we managed to track this back down so buffer sizes, just the maximum amount of data you're willing to accept
18:53
so we'll do. It's not received, but for size of 1000.
19:00
Well, we'll do 10. 24 because this is a computer and
19:03
powers of two were easier.
19:06
Eso Yet for reasons that aren't super important, Uh, 10. 24 is a more optimized number than 1000 when you're doing programming. Always try and use powers of two when you're talking about data field data size. But that's not super important right now anyway. So let's give this yet another crack.
19:22
Hey,
19:23
now it's running something
19:26
different error.
19:32
This time we're not able to get a connection. Well, let's see why
19:36
our German string server over here doesn't seem to be running anymore.
19:42
That over
19:44
from that
19:48
Ah, that's the problem.
19:49
We're trying to run them both.
19:52
That's not important. We're trying to run them both in the same
20:00
command prompt.
20:02
So what we're going to do instead is we're real quick. We're gonna do some,
20:07
we're gonna open it Instead, we're going to go to Python.
20:11
We gotta find this file, actually. So you're gonna go and you're gonna go to your documents
20:15
or wherever you're storing these files, I'm storing mine and a folder called Jeannie
20:23
Journeymen.
20:26
And then we're just going to do python
20:33
there. You go.
20:34
Ah, Journeymen String Server.
20:37
So
20:40
as is what, as happens often when working with Python working with I d ease you run into some weird courts by the I. T. Jeannie's. One of Jeannie's weird quirks is that you can only run one Python script at a time,
20:51
so we just have to run this one manually.
20:55
We have that running.
20:59
Go over here back to our activity, sister, and we hit F five
21:03
and test was a success. Test three failed.
21:11
Here we go.
21:14
Socket is not connecting.
21:19
So for some reason, it failed.
21:22
So let's take a quick look at why.
21:29
Turns out, actually, uh, the reason it failed. It just paused and check. My other code is that the server was slightly Ms Written. It'll be correct when it gets to you, of course. But I had a little psycho. So let's try this again.
21:41
This time it should work just fine.
21:41
So you run our script and we hit a five
21:45
and bingo,
21:47
You see that returned off of Bravo Charlie Delta.
21:49
So, barring you know, human error in the
21:52
server,
21:53
we find that networking and python is actually pretty easy to connect and get information only took four lines.
22:00
And, um,
22:02
the important thing in socket workers and anything really is
22:06
knowing you know where to look for help The pie, Thunder Python help functions are very important
22:11
as well as obviously Googling on and then the python manuals.
22:18
But yeah, that's all there really is to it for networking and Python. And with that, we've completed 1.3.
22:23
So the next 11.4 is working with classes.
22:29
Classes are one of the things that make python extremely import.
22:33
Um, classes and object, as we discussed, obviously in the slides. But quick summary of class is an object,
22:40
and it is
22:41
sort of the one of the many cornerstones of python.
22:45
So what we're going to do with class and this one is we're going to create a class called person,
22:48
and that class is going to have the attributes of height, weight, hair color and eye color.
22:52
And they're going to implement it to describe yourself or some other mythical person if you don't want to describe yourself or whatever, is it?
22:59
Ah,
23:03
so the first thing we've got to do obviously, is class,
23:07
and that's a python keyword. Everything in Python. All classes and python are generated from a sort of base class, which just is something which just exists. So we do a class
23:17
person.
23:21
And then we started adding fields so self
23:25
dot
23:27
equal zero self doubt. Wait
23:30
zero self dot
23:33
hair equals and empty string
23:37
and self dots.
23:40
I equals an empty strings,
23:41
and that's the basic class. So that's that's the contents of the class. Obviously, we're going to do a little bit more.
23:48
So the first thing you've got to do it
23:51
any class
23:52
is initialized that class. Pardon me
23:57
and what you initialize the way you initialize rather classes and Python is with the
24:03
innit function.
24:07
So the first thing we're gonna do is self. So every function in a class has self as argument, and that's just a sort of pretty fine thing, which is the, um,
24:18
the object of the class itself so that your functions, your methods,
24:22
which is what this is called, actually. So put that in there.
24:26
This is a method.
24:29
No, they're referring to their own function and not something wild and wacky.
24:33
So So
24:36
hi,
24:37
Wait
24:37
hair
24:38
Bye.
24:44
And then that's the functions of colon at the end
24:48
and we do self
24:51
hi
24:53
equals height
24:56
and same for all of them.
25:00
Pretty straightforward. Pretty simple
25:03
hair self dot I
25:07
Hi.
25:10
So now when you create a class
25:15
on instant in instance of a class, you can actually
25:18
fill it out.
25:19
So, for example, with new class, Billy will do Billy the incense e ation of class,
25:26
person
25:26
in class person again. We have height, weight, hair, and I we don't actually given an argument for self, for innit? That's something that it just needs without us doing anything so high. Well, so we'll save that Billy is
25:37
six feet tall.
25:40
Wait will say that he is £180.
25:45
Hair color is black
25:48
and I color is
25:51
blue.
25:56
And then we're going to try it out, and we're gonna just see
25:59
if that's you know, if all of this works, right,
26:02
So print billy dot height,
26:04
print,
26:06
billy dot wheat.
26:10
And so on and so forth.
26:12
Hair,
26:15
I.
26:25
So, for this one, we're actually not going to the tester. Doesn't actually give you any information to test
26:30
Um, because obviously
26:33
it would be kind of hard to test to make sure that you're putting in the exact right things. So instead it's just gonna run it. And the print statements that we put in are gonna help us make sure that we have in San created our class properly
26:45
closed this.
26:45
So we're gonna hit at five, and it's going to sprint through all of our tests. Well,
26:51
one thing we are gonna want to dio and this is the easiest way to get past it is after you've completed test three just comment that out.
26:59
Um, you can just run your server over here
27:02
every time you test,
27:03
but that sort of time consuming and it slows down to use up a lot of resources. So I recommend you just comment out test three when you when you've successfully completed it,
27:12
go ahead at five.
27:15
And it says self is not defined.
27:18
Okay,
27:23
What is that?
27:26
Well, let's have a look.
27:26
So we see that we have all of these fields being populated in here, But the air that it gave us was that self is undefined.
27:33
The reason for this is because the first thing
27:36
accidentally moved these Sorry.
27:37
The first thing you have to d'oh
27:41
on the classes to find is initializing this has to come before anything else. Because then it is a built in
27:48
ah has certain built in qualities and python,
27:51
which will again initialize self.
27:53
So where we tried to give it default values up here for all the self fields, those don't work. We can't do that.
28:00
Um,
28:00
but say we wanted to have sort of just the native class person, just in case we don't want to actually put anything in it
28:07
that is possible to do.
28:08
And that's why giving it
28:11
God,
28:11
basically, it's by giving it default values. So what we do is we go in here and in the actual function arguments,
28:19
we set these values.
28:22
So what this is saying is, if you don't receive anything for this value if you don't get anything,
28:27
um, for this argument,
28:30
that's fine. Just fill it with what's already here in the argument
28:34
in the function arguments.
28:37
So save that we're gonna go over to our activity sister again, we're gonna test it.
28:41
Expected an indented block,
28:48
and
28:52
and there you go.
28:53
61 80 black blue.
28:59
It's all it's doing here is
29:00
creating an object based on the arguments were giving it right here,
29:06
So the initialization is done, but
29:07
do we really wanna have to do a separate print statement
29:11
for every single thing?
29:14
Wouldn't it be much easier
29:15
if we just did
29:17
death
29:18
print info?
29:22
We, of course, after I give itself
29:27
and then we can use to print
29:29
and we can actually, you know,
29:32
percent D
29:33
and safety
29:36
and
29:37
s and
29:40
percent s. And we can actually make it a little bit easier to read by putting in the actual values
30:02
that Colin
30:03
hair
30:08
and I
30:15
and then we give it the arguments.
30:17
Self doubt,
30:22
self wait,
30:26
soap dot hair
30:27
and self dot
30:30
I.
30:33
And instead of printing, all of these were just going to do
30:37
billy dot print info. We're going to actually just call that macro directly.
30:49
But Colin to the end of death
30:52
of the function definition there.
30:53
Right? And look at that. It printed out just fine.
30:59
Now, some of you may be slightly confused if you were paying attention
31:03
by the fact that these
31:04
or different functions
31:07
and get these arguments are these variables are the same through them.
31:11
If you think back to the activity section or to the activities the Apprentice activities section there we go,
31:18
um, in the final activity for that, you had to define something is global if you wanted to use it in different functions. And this is where one of the really useful things about classes comes in
31:29
in the class has these values as sort of being global within itself.
31:33
Any method of that class and he function within the class can use the same variables.
31:38
That's great. And it's potentially dangerous if you're not paying attention. You might assigned the same name to two variables, but in general is just really helpful. You don't have to declare global's or you don't have to use some special macro. You just say, Hey, use the height, weight, hair and I that we to find somewhere else or that we messed with somewhere else
31:55
and use it here.
31:57
So that's the last in the minor activities for Journeymen Python.
32:01
Um, it's
32:04
pretty straightforward for all of these. Of course. As always, if you have any questions or if you'd like anything cleared up or if you just like Maur challenges Aiken
32:13
usually script sum up pretty quickly.
32:15
Send me a message on the on my page on the side worry about 80 website.
32:20
And after this, you're gonna get to dig into the journeymen final activity.
32:24
So thanks for watching.

Up Next

Python for Security Professionals

This is the archived version of our new Python course! If you would like to view our newly updated course, scroll to the bottom, and click the link.

Instructed By

Instructor Profile Image
Joe Perry
Senior Technical Instructor at FireEye, Inc
Instructor