Placeholder Image

Subtitles section Play video

  • What's going on?

  • Everybody welcome back to another sockets with python tutorial video.

  • In this video, what we're really talking about is basically just building on the last video where we reached the point where we realized things were still little clunky when we have messages that are bigger than, um, the buffer size that we selected.

  • But also, how do we maintain this stream of data?

  • So because we're finding that the only way that we can finally get our full message is to issue a close which closes the socket and ends the client so we'd have to rerun the client, I say doesn't work.

  • So we did cover everything you need to send and receive data via socket.

  • So I'm sure lots of people are gonna leave us behind me like I've got everything I need.

  • But you probably don't because it's gonna get a lot more complicated than this, and or you're gonna be missing out on a ton of stuff that sockets have to offer you, um, with basically any time you want to send things bigger than your buffer size s o most of the socket tutorials that I've ever seen Don't even talk about how you build the message.

  • They it's usually get Chad example.

  • They use a buffer of, like 2048 bites, and they don't talk about what happens when you exceed that.

  • So anyways, not gonna do that to you guys.

  • So how do we actually do it?

  • How do you handle sockets that exceed your buffer?

  • But you don't want to have to close this connection.

  • You want to keep a stream open and all that generally, what you're gonna do is use a header.

  • Now, there's a 1,000,000 ways that we could do something with a header tool in.

  • Basically, a header is gonna notify your program How long is your message and maybe give some other information about your message.

  • But in this case, we really just want to inform the program.

  • Hey, I'm about to send you a message that it's 50 characters long.

  • Okay?

  • And then once the program receives that first initial header there, like Okay, I'm gonna wait for till I have 50 characters of data once I've got that boom.

  • Now I know that message is done.

  • Now I'm gonna be waiting for a header again.

  • And then the process repeats so there's lots of ways that we could build a header.

  • You could make a header, have some sort of fixed pattern, like begin hitter, you know, And then maybe end, you know, and message.

  • Or, you know, who knows?

  • You do something like that until and it would work fine.

  • Until people learned that that's what you used.

  • And then they would start adding that to their messages just to have a good old time.

  • So you don't want to do that.

  • So what should you or what could you do?

  • In one option is a fixed length header.

  • So you just always have a header that, you know, proceeds every new message.

  • And it says, How long is that message going to be?

  • And then that way your program knows exactly how much the way for then, when it's all done, it re looks for that fixed length header.

  • You know, nobody can really mess with that.

  • Okay, so that's what we're gonna do.

  • So now how do we do a fixed Linkletter.

  • Well, enter string formatting and python.

  • Welcome to the python Booth Looks okay.

  • So first let's just do it.

  • So you got a message message and that will be equal to our welcome to the server.

  • Right.

  • Um, now what we want to do is get the length of that and issue that in a fixed length header.

  • So you can't just say land message because that might be, you know, if it's under 100 it could be two characters, one character.

  • If it's greater than 103 characters, we needed to be a fixed length.

  • So how do we do that?

  • So they were gonna do that is first, you have to kind of decide Well, what is, like, the longest message we could ever receive.

  • Okay, I'm gonna go with 10.

  • So 10 would be That's one comma.

  • So it's 47 10.

  • So if you're thinking you're going to get a message longer than one billion characters do something even bigger.

  • Okay?

  • But for now, I'm gonna go with 10.

  • So what we're gonna say is Prince, and then this will be f so these f string here and then we're gonna say Len message.

  • And then, um and this will be colon less, Stan.

  • And then whatever that header length is in this case, we're gonna say it's 10 So now we'll print that out and you see it says 22.

  • But what you don't see is the extra characters.

  • But if I highlight it, you can see it.

  • It's a fixed length.

  • It's 10 characters.

  • The next thing we could do is we could actually append to it.

  • The original message should go do that.

  • And now you can see here it proceeds the message.

  • Now, if we said 20 it would, you know, proceed with 20 characters.

  • So again it could be as many as you need.

  • So you can understand that eventually you you could come up with a number and it's not gonna like, you know, require a huge like your buffer will always be more than enough to handle your header size.

  • Well, I guess I can't say always, but it should be Okay, Great.

  • So now well, we want to do is implement this.

  • Luckily, we have basically done it so at least server side.

  • So now I'm going to come down here cut, come down here a paste that we'll have that bad boy right on over message will equal the header plus the message.

  • Don't forget the plus message bit otherwise you're gonna have a bad day.

  • Cool.

  • Okay, we're done.

  • Server side.

  • Easy.

  • Now we have to come to our client.

  • And also let me changes back to 10 thing.

  • Other thing I'd like to do is implement a constant that is just going to behead her size.

  • And we're gonna set that to 10.

  • Um, and then here, we're gonna pass that with another.

  • What the hell happened?

  • Another set of brackets.

  • Cool.

  • Actually did not know this before.

  • Doing this to two rocks of abject.

  • Never needed to embed a variable inside of another variable.

  • Um, but yeah, you can just embed more brackets inside more brackets.

  • Fascinating.

  • Okay, save that.

  • Come over to our client and we're ready to implement this on our client.

  • So the first thing I would do is share the header size constant.

  • So paste that in there.

  • And now we are gonna continue with a new wow loop.

  • So again, while true full message, we're gonna tab this over not only when we have full message, but we also want to track new message as a flag.

  • So the very first message we get is definitely the start of a new message.

  • So we're gonna set that equal.

  • Too true.

  • We'll continue receiving in small amounts, but eight is way too small.

  • So we'll go with 10.

  • So we can at least receive the header, right?

  • Or actually, let's do 16.

  • So we receive a little more than the header.

  • So message we received the message I'm not gonna say is if new message.

  • If that's the case, we need to know how big is this message gonna be?

  • So if it is that we're gonna say prints and then we'll say knew message length, and that will be a message up to pet her size.

  • Okay, so that will give us basically the length, um, of the message.

  • And then what we're gonna say, um is, and it's sort of kind of dawning on me that I don't think I properly explained, like, what the heck is actually happening here?

  • So, uh, so obviously the 10 is how long This is your alignment.

  • So that means it's gonna be left aligned.

  • That's really it's more like an arrow.

  • So if you want to write a line, you could go right, and then I honestly forget what's There is a center I think it's like enter enter or something.

  • I could look it up.

  • Um, I am kind of curious.

  • Effort already forgot what it was.

  • Oh, no, it's a carrot.

  • And so it's a carrot sign.

  • So if you want it to be centered, you would use that.

  • But we don't want that.

  • It doesn't really matter, but we're gonna use left, aligned.

  • But I did.

  • Price should point that out.

  • If you go to the text version of editorial.

  • I linked to the string formatting example for Python.

  • Just because I see that is like such such a rarely used, uh, thing in Python.

  • So anyway, that's how you do it.

  • Okay, So, um, so now we get the message length, the next thing I wanna do is actually grab the message length.

  • So we're gonna say message length equals int, and then we can actually just parse the straight from this data.

  • So you a fur python in this case because that actually contains, like, some spaces.

  • Um, but python figures it out in other languages.

  • Like, if you're tryingto you can like sockets work the same, like, you know, doing stuff with my SQL or something or any SQL um, you can take your knowledge and go to other languages.

  • So but Python is magical in the sense that this is gonna work out for us.

  • Uh, in other languages, you would You need to do like a strip like a dot strip to get rid of those extra spaces like that.

  • This pride wouldn't work in another language, but you could do a dot strip in another language into it moving on.

  • So we have a message length and then we'll set.

  • New message is false because it's not any longer any message.

  • So now new message length.

  • We've got that information.

  • Then every loop we do wanna append to the full message, whatever the new message is.

  • And then if the length of full message minus the header size is equal to the message Len that were expecting, then we have full message, restive.

  • And then let's print Ah, full message header sized colon.

  • So we don't really care about the header.

  • We just want to see it.

  • Then finally, was a new message equals true and print full message.

  • Great.

  • Now the other thing I'd like to do is remove the client socket close we don't need that gnome or, uh, new message.

  • True.

  • And then also, um, we'd like to set full message equal to empty again, so we empty it out.

  • So if we got new messages, um, we would continue.

  • So let's say that save server.

  • I think we're ready to go.

  • Hopefully don't have any heirs.

  • We're gonna find out soon enough.

  • Server DuPuy Actually, I forget, actually, Shouldn't matter what version Python is.

  • Okay.

  • We already, uh Do I have it running?

  • Sore?

  • Okay, let me, uh let's up.

  • The sockets are the port rather.

  • So we'll do.

  • 1235 35 Come back over here.

  • And in fact, I'm just gonna exit that.

  • Let me just do it again.

  • Nice and clean server dupuy.

  • And then we'll do one more python clammed up high.

  • Um, okay, so it tried to convert that message to the end message, so it definitely Oh, I see what we've done, right?

  • So we build the message, but then we never actually converted it.

  • Okay, So converted.

  • Uh, darn it can't.

  • So, honestly, the only machine I've done this tutorial or I tried to fill in this tutorial on a boon to, um and I had a bunch of issues.

  • I always have issues filming on a boon to so then I tried to film it, or, uh, I've got my Mac.

  • I don't want to film it on a Mac, though.

  • It's just like a laptop.

  • And it's annoying toe, like, work on that in film client.

  • So I'm, like, legit, stuck with doing it where I can't break.

  • It's very frustrating.

  • Uh, anyway, um okay, so that worked.

  • We're done.

  • So now the connection has not been severed.

  • We are still sending and receiving the data and all that on we could continue to send and receive data.

  • So just to show that as a quick example, if we wanted to, we could come down here on Wow.

  • True.

  • So this is just like a really quick example of sending.

  • Um, like, for example, we could import time.

  • Uh, while true, uh, what we're gonna do is just issue a time dot sleep three, and then let's do copy paste.

  • Have that over this over the message will be, um, whips F, but it just doesn't matter.

  • Time to time.

  • Uh, the time is Okay.

  • Whatever.

  • So we send the time.

  • Um Then we get the header size message, and then all we want to do is send that message.

  • So copy this paste that save that.

  • And I believe we should be all set.

  • If not, we're gonna find out really quick.

  • But this is a great way for me to test that.

  • What I've said is true.

  • Server dot pie.

  • I really wish I didn't have to do is every time.

  • I really wish I could just break it.

  • Welcome to the server.

  • And then hopefully in three seconds, we'll see the time.

  • Congratulations.

  • We got it continued.

  • Continued.

  • Okay, so every three seconds will get this new message of how long the time is.

  • Hopefully one of these would be shorter sometimes the time like round itself, but it doesn't appear to be occurring.

  • But anyway, you get the idea.

  • Obviously, you wouldn't have to send the time.

  • This could be a stream of any information sensor data or chat data or, you know, server log information.

  • Who knows?

  • It could be anything.

  • Yes.

  • So this one was actually shorter.

  • So anyways, didn't see is working variable length, the buffers working.

  • Everything's great.

  • We're having a great day.

  • I think I just have one more thing I want to show you guys.

  • And I think I'll do that in the next tutorial.

  • And that is going to be how do we send?

  • Like messages like this are cool, like sending strings is awesome.

  • And you could use at least like Jason on or something like that.

  • Um, but in the next tutorial, I'd like to show you guys how to send python objects.

  • So how, like, let's say you've gotten a ray or a dictionary and didn't want egotism or anything.

  • Everything in Python is an object.

  • So basically any data structure, so including, like, machine learning models or anything doing this in Python is really easy.

  • So I want to show you guys the next video.

  • Um, exactly how to do that.

  • But before that quick shout to my most recent channel members, Raj Nish Kumar.

  • Cheeky buns.

  • 13.

  • Frank Lloyd Jr payments.

  • I, Addy and Mohammed all more.

  • Thank you very much for your support.

  • You guys are awesome.

  • Um, okay.

What's going on?

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it