In this video you're going to learn about DCP communication and how to build a DCP server and client
what the socket module and python.
Now networking is a very important aspect of device to device communication over the Internet learning
how to use the socket module which is built in the python standard library gives us a very practical
skill to code all sorts of things.
A network socket.
In this context is actually a process or a way by which we can make computers or devices talk to each
other.
Now you can think of a socket as a building block for network tools in Python and to get a deeper understanding
of networking and Python programming for networking.
I would recommend to further study the socket module from the Python.
Official documentation because it comes with numerous methods that give you plenty of freedom to build
whatever tools you can think of now outside of the socket module.
There are many Python modules for networking but I consider this one important since it allows us to
work at a fairly low level.
All right so our code today is going to be made of two parts.
So we have a server and a client.
The server is going to listen or wait for messages while the client is the one sending the messages.
So we'll start by coding the server and we'll import import not server but import socket which is the
module that we're using then we'll define the server address.
So let's say hosts equals socket guest host name.
Okay.
Now this method connects to local hosts then will specify a port number we want to listen on.
So we'll say fourth equals 9 3 3 7.
Just a random port number.
Now notice the host the host is going to be a string while the port in this case is an integer.
And this is all the socket module wants us to specify the address details.
Okay.
And then what we'll do is to create a socket object so let's say SOC on their sport equals socket light
socket.
And we're going to use socket f address family iiNet Internet and the type of socket is going to be
socket SOC under sports or soft underscored stream.
So this is for DCP communication.
Things like I said here f underscore iiNet is the address family for IP V for and soc underscore stream
specifies that we're going for a DCP connection and there's also SOC underscored that diagram which
is for you the IP often faster and less reliable.
Next.
We've been the socket.
We've just created to the host and the port variables that we've just defined so we do that with soc
underscored that then and we're going to say host port.
And as you can see it's taken as a tuple.
Now we could be more flexible here by allowing the port than the host to be given in the command line
and in that case we'd be using this as module and maybe we'll do that and another more advanced class.
OK so we've got a server defined now we need to actually have that less for incoming connections.
So we do that with soc underscore that.
Listen one one here denotes the backlog or the number of unaccepted connections before this server starts
refusing new connections.
Now let's also have something printed to the output so we'll say print on a new line server started.
Dot dot dot and then we'll go to a new line then we need to start accepting connections using the DOT
accept method.
So it will say Con a DDR equals SOC underscore dot accept.
Meaning that we accept connections from the client socket con and it's address a DDR once connection
is established and then we'll do another print saying connection established with
and then I'd say string a DDR then we'll send a message back to the client.
So first let's actually put this message into a variable so we'll say message equals on a new line.
Thank you for connecting not connection with connecting space and then plus string of a DDR and then
we're actually going to send this message with a coin that send
we send the message dot in codes or we need to specify the encoding because we're in Python 3 will encoded
in ASCII and then close the socket.
So con on that close.
OK so when we run the script is going to start listening for connections on Port 9 3 3 7 and whoever
connects it's going to send the message to and then immediately close the socket.
We could also run this in a while loop to listen continuously but will not do that here.
All right.
Next we'll go to.
But first let's actually save this and then we'll go to the client and call the decline side of it which
is relatively easier.
So we start with importing the socket module and then create a socket.
So SOC under sport socket that socket exactly the same thing as previously.
So socket dot.
Am I not.
Okay.
And the type of socket is going to be SOC underscore stream.
Then we can simply use the Connect method to connect to the server without wasting time defining variables
for the host and port.
Like with that in the server dot P Y.
Again like I said you can code the script such as in a way that it receives these values via the command
line.
So anyway in this case will connect to the server using SOC underscored that connect
and we simply pass on socket dot get post to name.
OK.
And then also the port 9 3 3 7 as a tuple.
So once connection is established we want to be able to receive whatever message the server sends.
So we define a variable message equals SOC underscore dot e CV V and how many bytes we want to receive
10 twenty.
Once that's done we'll close the socket with the SOC underscored our clothes and also print the message
we got from the server with print and as G the decode.
Of course ASCII control s and that's it.
A very basic DCP client server coded in python.
Now let's actually put it to work.
So I'm going to open up a command prompt and navigate to desktop where both the client and the server
are located and we'll start the server with Python server dot P Why says server started and then open
up a another command prompt put them side by side so that you can actually see how they work
so the server is listening and I also have to navigate to the desktop here.
And when I hit Python client thought P Y you're going to see how it connects to the server.
Send the message and we basically can't see the communication between the two of them.
So I'm going to hit enter.
Okay so we have a little error here is coquette.
So it's over here.
So they have socket control s then we go back go back over here and then do that one more time.
And as you can see the connection took place so when the client connected its connection established
with the IP and then it sent the message and it sent.
Thank you for connecting.
Awesome right.
So this was a pretty basic networking Doc through the socket module.
Of course you can further expand these codes by for example allowing ongoing lesson on the server side
using loops by receiving command line arguments by executing commands on the server from the client
by messaging back and forth between the server and the client like a basic chat application for example
by encrypting the communication and much much more.
So it's like I always say the only limit is your imagination.