In this video we program an example of a Python script to act as a TCP IP server, and a separate one to act as a client, and then we start both and see live transfer of data using the TCP IP protocol!
Don't forget to leave a like on the video if you found it useful, subscribe to the channel for tons more great content, and leave a comment letting me know with any questions or what you want to see next!
TIMESTAMPS: 00:00 Introduction to TCP IP in Python 01:10 Creating a TCP IP Server in Python 12:34 Creating a TCP IP Client in Python 18:56 Communicating Between our TCP IP Server and Client Programs!
hello and welcome back to the channel in
today's video I'm going to show you how
to use Python to act as a TCP IP server
or
[Music]
client so I'm going to go through this
code with you line by line and talk a
little bit about tcpip protocol along
the way basically an Internet Protocol
for messaging between devices where
there is a client server relationship
the server is really the active
component and the client is kind of the
passive component that's an
oversimplification but hopefully that's
enough of an explanation that we can
start talking about this project this
sort of concept definitely sort of
straddles between the realm of like the
It world and the programmer world and in
my case like an automation engineer
World um but we're going to go through
line by line we're going to start by
taking a look at how to set up a TCP IP
server script in Python and then we'll
take a look at how to set up a TCP IP uh
client script and then we'll actually
look at how running those two at the
same time can connect even on one
machine okay so let's get started by uh
creating a
simple TCP IP server all right and so
the uh module really the only module we
need to do this sort of thing in Python
is called socket so we want to import
the socket and then we actually want to
create 8
atcp IP socket so we imported the module
called socket that has the functionality
to allow us to uh create a tcpip socket
but we haven't done anything with it yet
and so what we'll do is we will say sock
equals socket the module and then dot
socket the uh uh command within the
socket module and then these two
parameters in here um socket.
ainet and then socket. sock stream this
is where um I'm going to be perfectly
honest I'm not a network expert these
are the parameters I've always used to
get this working and so these are the
parameters I always put in here and it's
worked thus far um I promise if you want
to dig a level deeper into the socket
modules documentation you can get a
better explanation of what each of those
are doing um but then we want to bind
the socket to the specific port
for the server okay so um the way port
for the server the way tcpip connections
work is the server has an IP address and
a port that both the client and the
server need to know about and then the
client is kind of choosing any open port
on its side when it connects if that
makes sense so the port only has to be
hardcoded for the client in the server
to listen to as it relates reles to the
server let's keep programming I think
it'll make more sense when you um see it
written out but so we'll go ahead and
we'll say that the server address is
equal to and in this case I want it to
run right on this machine um so I'm
going to say Local Host you could also
throw the IP address of your computer in
here instead of Local Host so my
computer's like
192168 1.64 or something right now I
could put that there instead I'm going
to say Local Host and then I'm going to
um just throw Port 10,000 on there
they're all I'm also going to spell
address correctly there are something
like
62,000 ports that you can use um so just
pick a memorable one um I'm going to use
10,000 and then let's print a command
something that is very smart to do when
you're dealing with like networking and
stuff that could kind of go wrong at any
moment is give yourself a lot of print
statement and so what we'll say is we're
starting up uh on and then we'll use two
variables in this uh and then we'll say
what uh connection the server address
has so this will tell us the host IP
address and the port that we're using um
and this sort of uh format for passing
in um variables into a formatted string
is a little bit different than I
honestly typically use I usually use
formatted strings and then curly
brackets um but I'm not going to lie
there's some really good stack Overflow
uh code that I was able to use as the
source of a lot of this and they did
their prints this way so I'm doing them
this way so all we've done thus far
really is we've binded bound the server
address which is the uh IP address or
the host and then the port and we've
bound it to the socket instance that we
created then we need to listen for
inbound connections and then we want to
just use sock. listen and then one you
could do five you could mess with the
variables in there um sock. listen one
is good enough for rock and roll so then
what we want to do is we want to wait
for a connection and we just want to
keep uh listening really so the server
um does pass data outbound but it's also
just wall true we want to wait for a
connection because there's not
necessarily a guarantee when you have a
server that there'll be any clients
pulling from it so we want to um
basically just wait for a connection to
come in all right but then what we'll do
is we'll say print waiting for
connection and then we
will say connection and the client
address are the two things that we're
going to pull back down from sock.
except so another command within the
socket module is sock. accept which is
basically us saying okay when something
hits our server we're going to accept it
and we're going to keep track of who's
connected to us in this connection and
client address variable then what we'll
try is we will
print connection from and then we will
say the client address so this will be a
little print statement telling us who's
connected to us and as you'll see when
we set up the client pie in the second
half of this tutorial you'll see uh
exactly where that comes from and what
it looks like so what we want to do is
we want to
receive receive man that's a tricky one
to spell receive the data in chunks of
bytes um and so to do that what we want
to say is while true again so now we
have tried to uh connect and if we've
got a client address then what we're
going to do is we're going to say okay
the data is equal to whatever we're
receiving connection. receive from that
port and uh this is where you define how
many bytes the buffer size you want to
pull in in one message um I've seen this
be like 16 bytes I've seen it be quite a
bit larger so this um is supposed to be
specific is to the size of the messaging
that you'll be receiving
but in any case you can make it a really
large V value because uh at worst case
you'll receive everything that's in
there so then we'll say we've received
and then percent s here and then what
we'll do is we'll pass in all of the
data that we received Okay so we've got
this going on and now what we're going
to do is we're going to say if there's
data so if in fact we have received
anything over that connection then what
I want to do is I want to send it back
to the client so that way we can show
the uh data to and from uh tcpip using
like an echo listener client uh server
relationship so sending data back to
client um and what we're going to have
at this point is a bytes object so it's
going to look like a string if you were
to just print it out which we do up here
it's going to look like a string but
it's technically bytes and so what we
want to do if we want to be able to
manip manipulate it like a string we
want to do string of data um and then we
can start adding stuff to the message so
let's say that I want to send it back as
whatever the original message was and
then I want to say plus extra from the
server so in the world of programming if
all the client was sending to the server
was Hello World then this might be Hello
World Plus extra from the server and
then going back to um bytes format
because that is how we send messages
over
tcpip so I know we're moving kind of
fast but um if you have any questions
about the specifics that you see during
this tutorial let me know in the
comments below because there is a lot to
cover and I don't want it to be like a
crazy long tutorial um so what we're
doing
here is we are taking all of the data
that we get from the client we are
adding a little bit of text to indicate
that the server did read it and is
sending it back then we're converting it
back to bytes using this encoding
protocol called utf8 it's a method of
encoding strings into bytes and then we
send it back over the same connection
now because we had an if we want an else
and if we don't get any data we want to
print no more data from and then the
client address so this is just a way of
saying hey we're pretty sure we've read
everything that was available there so
we'll do connection. close to um clean
this code up and then we'll do a
break and that should Escape our while
loop and then because we have a try up
top we can throw a finally down here and
we'll just say clean up the connection
and I'm sorry I think I misspoke earlier
connection. close this is where we want
connection. close in our finally code we
do not need it here although strictly
speaking I don't think it would hurt
anything there um and all of the code
that you're seeing here for both the
server and the client will be in a
GitHub um Linked In the description of
this video as well so if you'd like to
not have to follow along line by line it
will all be linked below as well so you
can go and grab it directly um but what
we have done here to Spark Notes speed
run through it we imported the socket
module we created an instance of a
socket stored it in this variable stock
we defined our server address which for
our application will be this machine and
Port 10,000 and then we bind the server
address to our socket item and then we
are listening for inbound connections
when we get a connection we store it in
this connection comma client addresses
those two variables we display who's
connected to us and then we start
receiving data and when we receive data
over the port through
tcpip uh if we get data we add a little
bit of text onto it to basically say hey
we got this and then pass it back to the
client by encoding it as bytes and then
when there's no more data be to be
processed we close it um but it will
return to the outer while loop which
will continue waiting for a connection
because the nature of a server is you
don't necessarily want it to close every
time you close a connection with a
client if that makes sense so you want
the server to keep running even as
clients pop onto the network can pop off
of the network so let's go ahead and
table this right now because if we were
to run it right now all you're going to
get is okay we're starting up the local
host and now we're waiting for a
connection because we haven't created
the client yet and I don't have a
machine um that is a TCP IP client
hooked up to my laptop we're not going
to hear anything over that connection
yet so let's put a pin on this uh the
server side and let's write the client
side together and then we'll run both of
them and we'll talk about what we've
built once they're both written all
right so now let's go ahead and create a
TCP IP socket uh for a client all right
and the uh beginning is going to look
very quick uh very similar so I'm going
to go pretty quick where sock is still
equal to socket the
module. socket and then still socket do
AF
inet and
socket. sock sock stream there we go and
then what we want to do is we want to
connect the socket to the port where the
server is
listening okay so the line is going to
look exactly the same server address
equals Local
Host and then Port 10,000 the reason for
this is like we talked about a little
bit earlier the client only needs to
know the IP address and the uh the port
that it needs to use on the server the
client assuming your uh TCP IP server
does not have like a bunch of security
or firewall rules on it the server is
the one that has a hard programmed port
number and IP address and it's going to
receive dat from clients and it's kind
of like a surprise what the IP address
and the port of the client that are
getting used are so the server is the
one you have to Define both on the
server side and the client side so what
we'll do on this side rather than saying
um starting up a server we'll say that
we are connecting to and then very
similar to uh the command we'll say
we're connecting to this um address and
this port and then we will just pass in
the server address server address
parameters okay so that'll still give us
some useful information in the console
window and then we'll do socket. connect
to the server address okay and what
we'll do now is we will try
to send data to the
server all right so we want to try to
send data and we'll say in this case
it'll be a message and it'll just say
this is the message uh and then we will
say please repeat so we're basically
asking the
server to read our message and send it
back to us and then just so that the
print command knows we will say that we
are trying to send and then uh variable
and then we will say we are trying to
send this message and maybe to show that
it's a quote we'll do that um print send
message I think I missed a space there
we go all right and moving on we'll say
sock. send all okay so send everything
that is in that message but if you
remember because that is a string we
need to encode our message in utf8
format and turn it into bytes so we can
do this just like putting Str before
variable makes sure it's a string
putting bytes before variable and then
an encoding protocol makes sure um that
it is a bytes
object okay so now we will look for a
response back okay and we'll say the
amount that we have received starts out
as zero and the amount that we
expected
expected is going to be at least the
length of the message okay so we don't
necessarily know on the client side if
the server is going to be sending
anything back or not and these ones are
kind of specific for the application I'm
setting up here which is an echo client
basically saying I'm going to send data
I would like you to send the data back
more or less just to establish the
connection
but what we'll say is while the amount
that we've received is less than the
amount that we expected that means that
we want to keep listening so data equals
sock. receive and we'll say 1,600 right
because that was kind of the max that we
uh said we were going to receive on the
server side and then what we'll say is
okay the amount received just went up by
the length of the data that we sent so
this is just a track er um more or less
keeping track of how much inbound data
have we received and we'll print out
everything that we've received to show
that we got it back on the client
side and
then
data and then we will add just like uh
just like in the server side we had a
try and a finally we will say in our
finally that we are going to print
closing socket okay so to give an
example of the client whose mission is
basically connect to the server to do a
specific task or pass a specific piece
of data um and then when the work is
done it's okay to do sock. close because
you're basically saying hey the socket
that I was just using until I need to
connect to the server again I don't need
that connection anymore so I'm just
going to go ahead and close it the
server's job is to be open and uh
constantly kind of listening for
connections on that port and the
client's job is basically to connect
when it needs to connect um and send
data went needs to send
data okay so uh I know there was a lot
in this again I know industrial
networking and not even industrial
networking really this is just like
networking in general um I know this is
a little bit different than just
straight programming but this is a
really important concept especially as
you get into communication protocols so
let's go ahead and run some tests here I
think I can get kind of a crafty view
here where we can see our server on the
left nope that's also the client here we
go we can see our server on the left and
we can see our client on the right and
I'm going to make the text a little bit
smaller um I know if you're watching on
like a phone this is probably a lot
harder to read but it was big while we
were programming so um I'm you know
that's half a win so here's the order
we're going to do things in we're going
to kick on our server right now and in
the window you can see we get get all
right I've started up on this local host
on this port and I'm waiting for a
connection now if we did this right when
I run the client over here run client so
you can see some action in the terminal
window of the server it was waiting for
a connection oh I got a connection from
and then this uh
12701 is really the loopback IP meaning
same device but here you can see like
this is the IP address and some random
Port that the client choose chose to
connect to the server and so we say hey
we got this message this is the message
please repeat so we decide to send data
back to the client then we get an empty
message there was no more message so the
socket closed and now we're waiting for
a connection again that's what happened
on the server
side usually you would use the data that
you received to do more than just kick
it back to the client as an Echo that's
where you would do something useful with
the data but for the sake of explaining
how to use a TCP server I think we've
covered it let's take a look at what the
client was doing we were connecting to
and it could see Local Host on Port
10,000 then once we were connected we
sent this is the message please repeat
and then what we got back and you can
see these B's in the beginning because
we formatted it as bytes so if you want
to get rid of that all you have to do is
a little bit of like string uh data
cleanup but what we got back was this is
the message please repeat plus extra
from the server so you can see the
client sent a message over there and
then received back the message as an
echo plus the extra message from the
server and then we closed the client so
what's really cool I have not restarted
the server but the client program
stopped the script stopped because it
sent the message over that it wanted to
send so what we can say now is new
message please repeat also um and I have
not touched the server script it's
stayed running the whole time but if I
run the client again now you can see the
server just said oh it was waiting for a
connection it got a new connection and
what you can see here is a symptom of
how clients work it used a different
port this time because the client is
just going to grab an open port and then
it said oh hey I received new message
please repeat also I'm going to send it
back and then I had no more data and
then the the connection closed and now
I'm waiting for a new connection and
what happened on the client was I'm
sending new message please repeat also
and then I received back new message
please repeat also plus extra from the
server okay so I know that this covered
a lot of material and I know that I kept
it moving pretty quick again the code
will be linked in the GitHub below
um and I will get back to you uh on any
questions that you have as soon as I can
so be sure to drop those in the comments
below I thought this was a super
interesting concept it came up in my
work recently and so I was working on
some bigger like Arduino tutorials and
physical builds and I was like you know
what that was super useful for me and I
had a little bit of a hard time finding
useful resources on the internet so once
I figured it out I figured it would be
good subject matter for a video I hope
you guys uh enjoyed it be sure to let me
know in the comments below what you'd
like to see more of on the channel thank
you to everyone for continuing to
support L Master Tech a massive thank
you as always to my patreon supporters
good luck with your projects thanks for
watching and until next time bye