Placeholder Image

Subtitles section Play video

  • what is going on?

  • Everybody.

  • And welcome to a sockets with python tutorial.

  • Siri's in this video.

  • We're gonna be working with sockets.

  • So we got a lot of stuff to cover, so let's get into it.

  • So I've got a serve it up high in a client dot pine already created.

  • So if you haven't done that, which probably haven't go ahead and make two files and we're gonna get started in the server ID up high.

  • So to get started, we're going to import socket Now.

  • Socket is part of your standard library, so you won't need to install that or anything like that.

  • You already have it.

  • Now we want to do is define our socket objects.

  • So s equals socket dot socket and the socket family type is a f i net.

  • And then the actual type of socket is going to be socket dot sock stream.

  • Now, if I net corresponds to I p V four and socks stream corresponds to TCP, So this will be a streaming socket.

  • And if anybody knows what a f net like, why Why is it called?

  • If I know like, why don't we just say socket dot I don't know.

  • I p v four let me know down below.

  • Really curious.

  • So anyways, it is what it is.

  • So that's our socket object.

  • Now what we want to do is bind that socket.

  • So s dot bind, and we want to bind it to a to pull based on.

  • Sometimes this will be different.

  • But anyway, the Tupelo will be in this case because of the type of socket, and I pee in a port.

  • Now, this is our server.

  • So we're actually just gonna host the server on the same machine that we have the code.

  • So we're gonna bind to socket dot Get host name.

  • So whatever the host name is, basically local host and then the port.

  • We're gonna go with 1234 You can go with any pre match four digit port.

  • Lower digits are generally gonna be already occupied by some other program, and you just don't want to step on their toes and more likely they're going to step on your toes.

  • So once we've bound that socket also, let's just talk real briefly.

  • What even is a socket?

  • So?

  • So socket is just the end point.

  • Um, generally gonna like two end points, right to have a communication.

  • So that's just the endpoint that receives data.

  • So with a socket, you send and you receive data.

  • So the socket itself is not the communication.

  • It's just the end point that, like, receives that communication on that end point sits at an I p in a port, so we will stop bind that socket.

  • Now what we wanna do is s dot Lissy, this is our server.

  • We're gonna be making some connections to the server.

  • So this server has to be prepared for these incoming connections, and this server will prepare and leave a queue of five.

  • So if it's under some sort of heavy load and like multiple connections air coming in so fast that it can't respond quickly enough and they started stacking up, we'll have a queue of five.

  • Should be more than enough for our purposes.

  • So then we were going to say is now we're just gonna listen basically forever for connections.

  • And if we get one witness, a client socket and then address equals s dot except so anybody connects.

  • Hey, we're happy to see you were gonna connect.

  • Uh, and then we're going to store the client socket object into this variable client socket.

  • And then address is where are they coming from?

  • So this will be like their I p address.

  • Basically, so s so what?

  • We can accept this and then the clients socket.

  • This is just another socket object much like this.

  • Sock an object here.

  • Weaken.

  • Send information to the socket we can.

  • This socket will receive information as we will eventually describe in client dot pie.

  • Okay, cool.

  • So once we've got that connection, let's just print some general debugging f strings.

  • Um, where does a connection from?

  • Ah, address has been stab established exclamation mark because this is serious business.

  • Okay, so now we're gonna do is client socket dot send.

  • So we're actually going to send information to the client socket, which I feel like is weird, Like the client socket is a socket object, but it's like a foreign socket objects, so we're going to send information to it, But I almost feel like I don't know, it would make more sense to me if, like, we did s dot send and then we passed in client socket.

  • This feels weird to me, but Anyways, this is like our local version of the client socket.

  • So to send information to that client, we're gonna say client socket dot send What do we want to send to this person?

  • So we're gonna say bites.

  • You also could just do like a string and then dot and code or whatever.

  • Anyway, is there to say, here, welcome to the server.

  • And then what type of bites are these?

  • These air utf bites utf eight bites.

  • So now that's pretty much all we need to do to send some information, so I'll save that.

  • Now, this is not nearly enough for regular use, But this a basic examples base because I could make it.

  • So now we're gonna work on our client dot pies.

  • So again, client is much like server.

  • It's gonna use the same exact socket.

  • So I'm actually gonna copy paste that information now, this socket rather than binding instead, what this socket really wants to do is connect, So we're gonna say s dot connect and then we pass a to pull again of the I p in the port that we wish to connect to in this case.

  • It's socket dot Get host name and the I P same is 41234 Now often is the case.

  • Your client will be like remote to your server.

  • It won't actually be on the same machine.

  • So with sockets you can communicate to and from python programs on the same machine on, like a locally networked set of machines or even remotely networked machines.

  • So it just doesn't matter.

  • In most cases, you're probably not actually going to use socket dot get host name or at least not all of the cases.

  • You would probably won't do that.

  • You'll connect to an actual public I pee or something like that or a look alike.

  • But for now, we're gonna do everything on the same machine so everybody can follow along.

  • Uh, cool.

  • So we make our connection and, um, we could be done, but we're gonna accept that message that was sent to us.

  • So we're to say message equals s dot ar e c v for receive and we're gonna receive.

  • And this is our buffer.

  • So remember this TCP socket here?

  • This is a stream of data.

  • So you've got a stream you have to decide in how big of chump chunk chunks.

  • How many chumps?

  • Er, how big chunks of data do we want to receive at a time?

  • So in this case, 10.

  • 24 that's more than pig Enough.

  • But it really depends on what you're doing.

  • Like if you're sending massive files, you wouldn't want a really tiny buffer size.

  • You might want something a little larger.

  • So and then it also depends on the application that you're then going to use it with and stuff that this gives could get really complicated really fast.

  • Like if you're really into, like, networking and stuff, uh, you could be paid a decent amount of money.

  • Like to come up with the proper amounts for these things.

  • But for now, 10.

  • 24 good enough.

  • So now we're going to say is, uh, just prints message dot decode and this was UT F A code.

  • And again, the way sockets are communicating at the moment, at least with the type of socket that we've established here is a byte stream.

  • So we're receiving these bites.

  • So they're sent his bites that received his bites, and then we just do code the bites.

  • So in this case, we should get the message saved everything.

  • So now all we need to do is run it.

  • So I'm gonna do cmd here.

  • And, um I honestly is my local machine.

  • This is prime not 37 Yes.

  • So instead, what I'm to say is Pied ass tree seven shouldn't actually matter.

  • You should probably able to do it on any version of high three.

  • Anyway, run the server.

  • So the server is now running.

  • And now I just need to open up one more for our client pie Dash three point.

  • Hello.

  • 3.7 client dot Pie.

  • And I think what I'll do is all, like, I don't know.

  • I don't like that we connect.

  • We see.

  • Welcome to the server from our server.

  • And then here, uh, can we see it here?

  • Connection from our I P address has been established.

  • OK, so it looks like that worked for us.

  • But this also just, like immediately disconnected, which is probably not really what we're after.

  • You're probably looking for a more long term connection, but we could just, like, keep running that again, like every time we wanted to receive a bit of information, but that's kind of silly.

  • Most people aren't actually doing that with sockets.

  • They're going to be doing something far more complex.

  • So I think the last thing I want to show you guys before we, uh, leave and go to like, the next video is, um, you know, the way that truly sockets are gonna work in the way that you know, you're actually gonna be buffering data is most of the time.

  • You just like, can't expect that with this, um, that this will be enough.

  • So in this case, you could send just about any message as long as it was less than 10 24 bytes of data and you would be totally fine.

  • And you could you could set This is Bigas You could plausibly conceive, but that would be silly.

  • What really happens is again, it's a stream.

  • So I'm gonna set this toe eight and then hit save for the client, and I honestly don't remember, did we, uh, don't yet know we haven't done anything to the server, so I'll leave the server running.

  • And instead, let's rerun the client again and you'll see what we get is just welcome, right, Because we only we've received that one little itty bitty buffer.

  • But we're not actually handling for really anything here.

  • So it's actually what we want to dio is while true.

  • So wow, true.

  • We receive this data tablet over hit save.

  • Come back up here, rerun it, and we get this.

  • Welcome to this.

  • Over.

  • Right.

  • Okay, so, yeah, that's what happens when you buffer data.

  • But why do we have to buffer it?

  • I mean, because we could have gotten away with 10 24 but eventually someone's gonna try to send a message that's bigger than that.

  • And maybe on your chat, Lissy, make a chat app.

  • Maybe you're you set a character limit, and it becomes more challenging to do more than that.

  • But there's almost always gonna be a reason why it's gonna be better to buffer also.

  • I mean, in most cases, you're gonna be like, below, let's say certainly below, like, 10 megabyte buffer.

  • So if we're talking like files and stuff, you just you have to be able to understand how that's gonna work.

  • So then you start thinking, Well, that's not that hard man.

  • All we gotta do is something like this.

  • Like, I don't know full underscore message.

  • And then you just make that an empty string.

  • And then here, uh, you'll just say maybe something like this.

  • Like, uh, if, uh if you're actually what we'll do is under here, you would say, you know, if message is less than or equal to zero, right or lend message rather so length of the message is less than or equal to zero.

  • Uh, we're gonna break otherwise.

  • What we want to do is full message plus equals msg dot decode ut f a.

  • Who needs Centex, right?

  • Like, uh, so then after this, we could print full message, full massage, and then we come over here will break this one, maybe.

  • Well, I just okay yet, do York.

  • Uh, pi pu pi dash 3.7 kalai int dot pie.

  • We try to connect.

  • Nothing happens.

  • Okay.

  • Uh, So what's going on here?

  • Well, the problem is, we still have this connection from the server and the way we don't actually know when the connection is fully completed, right?

  • We're still actually the stream, and then the connection is still exists.

  • So then you come over here, maybe, and then I don't know, clients socket dot clothes I think is the proper handling for that.

  • Unfortunately, I don't think I can cancel this from running either.

  • It's really annoying.

  • Why can't I break sockets in running pie?

  • Dash 37 Server.

  • DuPuy, please don't tell me that it is a use.

  • Come over here.

  • Rerun client dot pie.

  • And then we see Welcome to the server and exits.

  • And so, in this case, we were actually able to buffer all of the data.

  • Uh, we didn't really need any fancy logic.

  • We received the message, and we buffered.

  • So what's the big idea?

  • Well, the big idea is it's meant to be a stream.

  • We're supposed to be accepting data on a big stream.

  • And if server gets something and it decides it wants to send some information, we're just simply not going to receive it.

  • Now, we could kind of treat this like a rest ful a p i or something, and like build data until the client reconnects, accepts the buffer and then clears out with the client God or something like that.

  • But that's not the way that we typically will work with sockets.

  • Instead, you're gonna likely send a bit of information in the header and inform the client how much information is to come.

  • So that's we're gonna be focusing focusing on in the next video.

  • If you got questions, comments, concerns, whatever.

  • Feel free to leave those below quick shoutout to my most recent channel.

  • There we go.

  • Couldn't click on it channel uh, members on the crux Miguel Lutz, Lautaro Alexander and Mary Python.

  • A love your last name.

  • Thank you guys very much for your support Allows me to keep doing what to do.

  • So thank you guys so much and thank you to everybody else.

  • Eventually, I'm gonna figure out a decent way.

  • Thio re thank people who have stuck around.

  • Some people are still with me for, like, well, over six months, I forget exactly what we're at right now for the max, but that's freaking awesome.

  • So anyways, I'll figure out a way at some point.

  • So anyways, in the next tutorial will talk about how do you actually will buffer and keep the stream open and all that and do some more cool stuff with sockets?

what is going on?

Subtitles and vocabulary

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