Placeholder Image

字幕列表 影片播放

  • What's going on?

  • Everybody, welcome to part five of the sockets.

  • Tutorial.

  • Siri's in this video are gonna do is continue with our chat room application.

  • And this time we're gonna code the client side of things and then we're gonna run it and fix all of these certain bugs that I've created, or at least some of them the ones that we find.

  • Okay, let's get started.

  • So what is our client do?

  • Our client really does, like, two things, right?

  • Or maybe three things.

  • So on immediate connection, the client tells the server what their user name is.

  • And then from that point forward, it's just like this infinite loop of if you, the client has a message to send, send that message but also receive messages from the server.

  • Now, just to keep it super simple, we're gonna do everything inside of the consul s.

  • So we're just gonna use input, which is gonna block the rest of the program from running.

  • So you'll have to either send an empty message or send a message to get updates later.

  • I would add some sort of u y to it.

  • I do have planned to do that, but that's not really a sockets tutorial anymore.

  • That's Ah, gooey tutorial.

  • So, anyway, let's get started.

  • So import socket, because obviously import select for the same reason as before an import air?

  • No.

  • So, uh, that should be everything we need.

  • What is there?

  • No.

  • What are we doing?

  • We're gonna use this to match specific area codes.

  • Talk about it when we get there.

  • But basically one of the things that we're gonna do is, um, try to receive messages until we can't.

  • But our when we know we can't receive them, we actually will get an error.

  • And we want to make sure that there's a really specific error and not some other error.

  • Okay, so we want to make sure that air is because there's no message received and not because who know something was wrong with that message or code or whatever.

  • So, anyways, we're gonna use this to match specific errors.

  • You'll see it when we get there, then, um, we're gonna have the exact same starting Constance header.

  • Length will make that 10 I p will be the same as it was before.

  • 1 27 001 port will make that 1234 again and then my user name or will be input, and we'll just say, a user name.

  • So we'll just grab this from the client.

  • Um, basically immediately when they join.

  • So now we're gonna create the socket.

  • So it's a client.

  • Underscore socket equals socket dot socket and then its socket dot address family, Internet.

  • I assume our net in this case socket dot All caps sock under scorer stream.

  • Okay, great client socket.

  • Good.

  • Now let's go ahead and connect to I p import.

  • So client socket dot com connect to the to pull I P port and then we're going to say client socket dot set blocking to be false.

  • This way, uh, the receive functionality won't be blocking.

  • All right, So said blocking false.

  • Ah, The next thing that we want to dio is begin to send in this this information to the server.

  • So, like I said before, this does like three major things.

  • The very first thing that we're going to do is send in that user name for the server.

  • So recall, um, somewhere here I was hoping to use her equals, receives the first message.

  • Yeah.

  • Yes.

  • So if This is the server socket.

  • This is the brand new user.

  • And then this is where we set that user and stuff like that.

  • So look at this.

  • This is where we are.

  • Please don't move that in this inner, like, if we're looking at where server is goingto first interact with client dot pie would be here.

  • Um well, sort of.

  • But at least he's this code.

  • Uh, actually, uh, we would first interact really here anyways continuing on.

  • So ah, what we want to say here is user name will be equal to my user name dot in code ut f ate, then user name underscore Header will be equal to an F string of the Len of user name.

  • Ah, okay.

  • Colon less Stan and then Heather Length in this case, which is 10 header length.

  • This always screws me like this is hard enough to remember how to do.

  • Um And then you add f string to it and it looks goofy, But anyways, I think it's good.

  • So we will end code utf eight.

  • Okay, that's our user name.

  • Uh, in the header.

  • So now we're gonna do is client underscore socket dot Send user name header had, er plus whatever that user name is.

  • Um, is that really not a pep Eight violation toe?

  • And what if I put the space?

  • Is that gonna be a pep A violin?

  • No.

  • I could do whatever I want, huh?

  • Interesting, Actually.

  • Don't know the answer to that.

  • I think you'd probably want spaces around your pluses.

  • That's my guess.

  • Anyway, so we sending that information once we've done that, that's, like, the only thing that isn't going to be in the loop.

  • Like we'll need that information one time.

  • We're just not gonna let people change their user name.

  • Um, so the next thing that we're going to do now is just generate forever.

  • So while true tour while true Ah, what we want to do here is, um, both send messages and receive messages.

  • So the first thing I'm gonna do here is say, any message that we input is just going to be an f string.

  • Uh, that's also input F string.

  • And then we'll just say the user name.

  • So my underscore user names.

  • So this was just kind of before the person types anything?

  • Um, it will just kind of It'll just look like their own user name, then they'll type.

  • And it'll look really pretty and stuff.

  • It'll give him the feel of a gooey, even though it's really not.

  • Okay, so now, um, someone might just press enter.

  • So just to get it another date, right?

  • Just to get past input so someone might or they might accidentally hit in her Who knows, Eh?

  • So what we want to do is be ableto handle for whether or not someone actually input anything at all.

  • So the first thing that we're gonna ask is if message So if this is not empty, here we go.

  • If if message then message will be equal to message, not in code.

  • And we will encode that t o ut f eight bites.

  • And then what we're gonna do is ah, we need to set message, header, message, header, And that's equal to F String of again Len rips.

  • Yeah, Len Message plan, message.

  • Ah, and then here colon less than says always bothers me to look through this.

  • So, like, this one will wind up the same way.

  • So hopefully header length.

  • Okay dot income comes in code to utf eight.

  • I don't know if we'll get if we get away with having spaces there, I kind of want them.

  • But I'm not sure if that will work for the string formatting.

  • OK, so that's our message header.

  • And now we want to send that message.

  • So clients socket dots, end message, message, header, plus whatever that message is.

  • Okay, so that's us sending in the message if we actually sent one, But again, we might actually just press enter here just to update the chat.

  • So regardless of whether or not there is a message, the next thing we want to do is try to receive all of these messages.

  • So this would be a while true loop that we're going to receive things, and actually I before E except after C fellas.

  • Okay, so we're gonna try to receive things here, But like I said, we're gonna do that basically, until we hidden error.

  • So because that's gonna be our logic.

  • We're actually going to purposely in case this because we're expecting an air at some point for sure.

  • So try.

  • Except, um and then, for now, a pass.

  • But we'll fill that in in a little bit, but while true, we're going to attempt to receive things.

  • So first of all, we're gonna say user name underscore Header equals client socket dot Receive header length.

  • And then, uh, if not Len user name header whips.

  • Amen.

  • If not Len Username Header means we basically didn't get any data.

  • For whatever reason, we're going to say print, um, connection closed by the server, and then we run system exit and force us.

  • Okay, so, uh, other than that, we want to convert that, um, that user name header to an int.

  • So what we're gonna say is user name underscore length equals wth e into value of user name header dot decode utf eight.

  • And then again, like before we'll throw in a strip because normally you'd have to do that.

  • But I don't think you do.

  • Here, user name length.

  • Okay, so we've got the user name length.

  • Now what?

  • We want to grab knowing that header.

  • You know, whatthe length of this message of user name alone would be Ah, we can actually grab the user name here.

  • So that's gonna be client socket dot Receive user name length dot D code utf eight utf eight.

  • Okay, so we've got the user name now.

  • Ah, what we need to get next is the message itself.

  • So coming down here, recall when we send information, we're sending this right.

  • We're sending the the user, the user header fall about the user name and the header just contains how long is the user name.

  • And then in that exact same message here.

  • But just right after it, we're going to receive the message header and data not recall.

  • This is coming in a stream.

  • So we've accepted this exact amount.

  • We've accepted the exact amount.

  • That is the header data, then the header than the user name data.

  • Based on that had username, hetero data.

  • Let's call it and then again now at the point of data will be exactly at the header for the actual message itself.

  • So now we're basically gonna do the exact same thing above.

  • So message header equals client socket dot Receive header length.

  • So we do basically this exact thing.

  • Then we're gonna come down here and, um and continue.

  • That s o now.

  • Um, no.

  • Uh, what did I do?

  • Hear?

  • Username header User named length equals int.

  • Uh uh Here, username header.

  • Yeah, That caught my eye.

  • Uh, so at least we solved one bug already.

  • It was like, Wait, you can't do that.

  • Uh, using the length.

  • Okay, then where you see them?

  • Is it?

  • Okay, so we're gonna basically do the exact same like these three lines here, so the message won't be sent.

  • So in this case, this would be if something went wrong there.

  • But in this case, we shouldn't need that same error, so message header.

  • Uh, now, what we need is a mess Length and the message length is tthe e int version of the message header.

  • Don't decode ut f a a and strip so decoded it.

  • That's what that word was, in case anybody wasn't sure.

  • Okay, Message length.

  • And now we finally congrats.

  • But the actual message itself, which will be exactly this right here.

  • Copy.

  • Pasta change to message length dot decode utf eight.

  • Okay, so we have the user name and we have the message.

  • So all we're gonna do now is print f string, and that will be the user name.

  • Followed buying.

  • Whatever the messages, I just can't wait to run this and see how much trouble we're in.

  • Okay, so um, basically gonna have, like, a couple of exceptions.

  • So except we'll just do exception as e.

  • And then we'll just print string the, um and we'll call this just general here, Um, cyst on exit.

  • Then, um will come down here and we're gonna accept.

  • And I owe error as he Except I owe air.

  • Uh, capital?

  • I was waiting for the color.

  • Okay.

  • Except I owe air as e.

  • Now, um, if ee dot erno is not equal.

  • So we've got two options here that we might see you be air no dot e again.

  • And then if he does not equal that or Thedot erno does not equal air?

  • No, don't e would block.

  • So again, these are These are the areas that we might see depending on operating system when there are no more messages to be received.

  • So, in this case, were it were like, we don't really want to handle for this necessarily, because, um, we're expecting so it.

  • But if it's not one of these things, then we'll just say print, uh, String e.

  • And we'll just say some sort of reading error.

  • Uh, and then whatever string is that way we know where we hit an issue.

  • And then also it's just run assist exit here because we're we're done.

  • So, um, otherwise if we hit it and it is one of these things we don't care.

  • All we really want to do is continue.

  • Cool.

  • So with that, we are, um, presumably done.

  • But I can almost guarantee we aren't done.

  • So let's go ahead and run the two things here.

  • So cmd uh, Python uh, what is it?

  • Uh, Will do server it up high.

  • So we get that running, I'll put this over here and then cmd python client dot pie will say user name is H S O.

  • Assumes I connected.

  • We at least got the accepted new connection.

  • Hello.

  • World Figured, uh, could not be close connection from H and then also a reading error on the part of client blocking.

  • Some could not be completed immediately.

  • So the first thing I'm gonna dio is head over to It's hard to say in this case.

  • It could be actually is something that we type Odin server, but we'll go to client.

  • So he said hello.

  • World Server didn't actually see it, but we're actually checking for the read.

  • Okay, so the issue is in this case, it's not either or situation it it's an and situation.

  • So if you put in or here, obviously you're always going to get it.

  • Um, so instead, it is being And so if the air is not this, um and it's not this, then we go here because this is for deferring by operating system.

  • That's the whole reason why we have this.

  • So not an or and And so save that.

  • And your server actually should you should be ableto keep your server running and then just go ahead and rerun.

  • Python client dot pie.

  • We use h again and the case.

  • We got any message?

  • Hey.

  • Okay, Okay, great.

  • So bringing over the server now we see.

  • Okay, we got the new connection.

  • The user name is H received message from H.

  • Hey.

  • Okay, so then we could add in one more user here.

  • Python client pie.

  • Well, say user name is C.

  • Hey, many exclamation marks.

  • And, um, if we come over to this one, this is our user name H.

  • And like we either Well, first of all, a server Got it.

  • But also here, we're not gonna get it because we're being blocked by input.

  • So if you just enter here, we'll see.

  • Okay, We actually were able to receive the message from C immediately.

  • And if we did not take any input, we would see things immediately.

  • So, for example, just for the fun of it, because it's just so fun.

  • Ah, one thing you could say or do would be like for the first guy.

  • Let's see, client or girl Who knows who they're?

  • Uh we could just say, um message equals, maybe get away with that, uh, python a client up pie.

  • We'll call this user reader, and then we'll come back over here and now, actually, bring messages back the python client dot pie, See?

  • Hey there.

  • Um, so this here, obviously our server basically does it, but this would just cleaned up a little bit just to show you that a client other than if you just commented out the, uh you know the message input the client actually does receive in a stream of data here, so see?

  • Hey there.

  • Then we could open up another one.

  • Python clammed up pie user name H.

  • Hey, what's up?

  • Okay, then you get.

  • Hey, what's up much?

  • Okay, Anyway, you get the idea.

  • So actually really surprised that, um, I didn't type out anything along the way, and those funky F strings worked on the server, so that's cool.

  • Uh, okay.

  • So really, that's it.

  • The next thing you would do is make some sort of really you y and not actually use the command line.

  • Just because it actually really is difficult to put things into the command line and have that be updated live?

  • Yes, there are ways, but it's all operating systems specific.

  • And I just didn't want to do that that much, Whereas you could use something like a gooey, you know, like decanter pike, you tea or whatever.

  • Um and you just want to have that problem anyway, So, yeah, I think that's probably next step would be to do something in the u y instead.

  • So as you might be able to guess, I will plan to be doing that.

  • So So, uh, anyway, if that's what you want to see it stay tuned for the future.

  • Otherwise, questions, comments, concerns, whatever you can leave him below.

  • Quick shoutout to my recent channel members, Michael Ivan off for seven months.

  • That's a really long time.

  • Thank you very much.

  • Chong.

  • Sam Park, New member.

  • Thank you.

  • Lily's easy.

  • Also a new member.

  • A You mix also a new member.

  • I sort of recognize that name.

  • I think that you're in discord or something.

  • That's probably I recognize that name.

  • Dave Senta.

  • Four months as well.

  • Thank you very much.

  • All of you guys, for your support.

  • You guys are freaking amazing.

  • Uh, okay.

  • Questions, comments.

  • Yet all that below.

  • If you've got suggestions, anything I did wrong here, leave those below.

  • Also, if you can think of a nice, clean way to make this operating system, um, agnostic, but also update live, including message input.

  • I'd love to see it.

  • I just It's like a huge rabbit hole that I don't really want to go down.

  • Um, for this, But anyway, that's it.

What's going on?

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

Socket聊天室客戶端 - 在Python中創建套接字哈拉程序。 (Socket Chatroom client - Creating chat application with sockets in Python)

  • 9 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字