what is going on everybody and welcome
to a sockets with Python tutorial series
in this video we're gonna be working
with sockets so we've got a lot of stuff
to cover so let's get into it so I've
got a server dot pi and a client dot pi
already created so if you haven't done
that we should probably haven't go ahead
and make two files and we're gonna get
started in the server dot pi so to get
started we're going to import socket now
socket is part of your standard
libraries so you won't need to install
that or anything like that you already
have it now what we want to do is define
our socket object so s equals socket dot
socket and the socket family type is AF
eye net and then the actual type of
socket is going to be socket dot sock
stream now a fi net corresponds to ipv4
and sock stream corresponds to TCP so
this will be a streaming socket and if
anybody knows what a fi net like why is
it called a fi net like why don't we
just say socket dot I don't know ipv4
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 to that socket so that's dot bind
and we want to bind it to a tuple based
on sometimes this will be different but
anyway the tuple will be in this case
because of the type of socket an IP and
a ports 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 get
hostname so whatever the host name is
basically localhost and then the port
we're gonna go with one two three four
you can go with any pretty much
four-digit port the lower digits are
generally gonna be already occupied by
some other program and you just want to
step on their toes and more likely
they're gonna 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 a socket is just the endpoint
generally you're gonna have like two
endpoints right to have a communication
so that's just the end
that receives data so with a socket you
send in 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 IP and a port so we will s
top bind that socket now what we want to
do is s dot listen this is our server
we're gonna be making some connections
to this server so 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 are coming in so fast that
it can't respond quickly enough and they
start stacking up we'll have a queue of
five should be more than enough for our
purposes so then what we're gonna say is
now we're just gonna listen basically
forever for our connections and if we
get one we're gonna say client socket
and then address equals s dot except so
anybody connects hey we're happy to see
you we're gonna connect and then we're
going to store the client socket object
on into this variable client socket and
then address is where are they coming
from so this will be like the IP address
basically so so what we can accept this
and then the client socket this is just
another socket object much like our this
socket object here we can send
information to this socket we can this
socket will receive information as we
will eventually describe in client pie
okay cool so once we've got that
connection let's just print some general
debugging F strings we're gonna say
connection from address has been
established exclamation mark because
this is serious business okay so now
we're gonna do is a 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 object 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 them for me
into that client we're gonna say client
socket dot send what do we want to send
to this person so we're gonna say bytes
you also could just do like a string and
then dot in code or whatever anyways
we're gonna say here welcome to the
server and then what type of bytes are
these these are UTF bytes utf-8 bytes 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 is a basic examples
basic as I could make it so now we're
going to work on our client dot pi
so again client is much like server it's
gonna use the same exact socket so I'm
actually going to 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
tuple again of the IP and the port that
we wish to connect to in this case if
its socket get hostname and the IP same
as for one two three four 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 gonna use socket
doc yet hostname or at least not all of
the cases you will probably won't do
that you'll connect to an actual public
IP or something like that or a local IP
but for now we're gonna do everything on
the same machine so everybody can follow
along cool so we make our connection and
we could be done but what we're gonna
accept that message that was sent to us
so we're gonna say message equals s dot
R ec v4 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
or how many chumps uh how big a chunks
of data do we want to receive at a time
so in this case 1024
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 gonna use
it with and stuff that this case can get
really complicated really fast like if
you're really into like networking and
stuff you could be paid a decent amount
of money like to come up with the proper
amounts for these things but anyway for
now 1024 good enough so now we're gonna
say is just print message decode and
this was utf-8 so we go to code and
again the the 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 bytes so they're
sent as bytes the received as bytes and
then we just decode the bytes 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 I
honestly this is my local machine this
is probably not 3/7 yes so instead what
I'm gonna say is PI - 3 7 it shouldn't
actually matter you should probably be
able to do it on any version of Pi 3
anyway
run the server so the server is now
running and now I just need to open up
one more for our client PI - 3 point
hello 3.7 client dot PI and I think what
I'll do is I'll like I don't know
something like that we connect we see
welcome to the server from our server
and then here
or can we see it here connection from
our IP address has been established okay
so it looks like that worked for us but
this is 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
gonna be doing something far more
complex so I think the last thing I want
to show you guys before we leave and go
to like the next video is 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 you just like can't expect that
with this that this will be enough so in
this case you could send just about any
message as long as it was less than 1024
bytes of data and you would be totally
fine and you could you could set this as
big as you could plausibly conceive but
that would be silly what really happens
is again it's a stream so I'm gonna set
this to 8 and then hit save for the
client and I honestly don't remember did
we I don't ya 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 received that one little itty-bitty
buffer but we're not actually handling
for really anything here so so actually
what we want to do is while true so Wow
true we receive this data so I'll tab
that over hit save come back up here
rerun it and we get this welcome to this
server right ok so yeah that's what
happens when you buffer data but why do
we have to buffer it I mean like because
we could have gotten away with 1024 but
eventually someone's gonna try to send a
message that's bigger than that and
maybe on your chat lesea make a chat app
maybe you're care you set a character
limit and it becomes more challenging to
do more than that but there's almost
always going to 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 got to 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
you'll just say maybe something like
this like if if or actually what we'll
do is under here you would say you know
if message is less than or equal to 0
right or lend message rather so if
length of the message is less than or
equal to 0 we're gonna break otherwise
what we want to do is full message plus
equals MSG dot decode utf-8 who needs
Centex right like duh so then after this
we could print full message full massage
and then we come over here we'll break
this one maybe wow I just okay
yet jerk pie puh puh - 3.7 client dot
pie we try to connect nothing happens
but okay so what's going on here well
the problem is we still have this
connection from the server and the we'd
we don't actually know when the
connection is fully completed right
we're still actually the stream and the
connection is still exists so then you
come over here maybe and then I don't
know client socket closed 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 PI -
3:7 server dot pi please don't tell me
that is a goose come over here rerun
client dot pi 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
and we didn't really need any fancy
logic we receive 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 and
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 restful api 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 going to likely
send a bit of information in a header
and inform the client how much
information is to come so that's we're
going to be focusing focusing on in the
next video if you got questions comments
concerns whatever feel free to leave
those below quick shout out to my most
recent channel there we go couldn't
click on it channel members omni crux
miguel lutz lautaro alexander and mary
python I love your last name thank you
guys very much for your support it
allows me to keep doing what I love to
do so thank you guys so much and thank
you to everybody else eventually I'm
gonna figure out a decent way to rethink
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 points so anyways in
the next tutorial we'll talk about how
do you actually will buffer and keep the
stream open and all that and do some
more cool stuff with sockets otherwise
I'll see you guys in another video