1 00:00:00,420 --> 00:00:06,840 In this video you're going to learn about DCP communication and how to build a DCP server and client 2 00:00:06,870 --> 00:00:09,920 what the socket module and python. 3 00:00:10,050 --> 00:00:16,470 Now networking is a very important aspect of device to device communication over the Internet learning 4 00:00:16,470 --> 00:00:22,830 how to use the socket module which is built in the python standard library gives us a very practical 5 00:00:22,830 --> 00:00:25,450 skill to code all sorts of things. 6 00:00:25,740 --> 00:00:28,040 A network socket. 7 00:00:28,080 --> 00:00:35,850 In this context is actually a process or a way by which we can make computers or devices talk to each 8 00:00:35,850 --> 00:00:36,200 other. 9 00:00:36,400 --> 00:00:45,840 Now you can think of a socket as a building block for network tools in Python and to get a deeper understanding 10 00:00:45,840 --> 00:00:49,370 of networking and Python programming for networking. 11 00:00:49,380 --> 00:00:52,980 I would recommend to further study the socket module from the Python. 12 00:00:52,980 --> 00:01:01,920 Official documentation because it comes with numerous methods that give you plenty of freedom to build 13 00:01:01,950 --> 00:01:06,270 whatever tools you can think of now outside of the socket module. 14 00:01:06,270 --> 00:01:14,880 There are many Python modules for networking but I consider this one important since it allows us to 15 00:01:14,880 --> 00:01:17,640 work at a fairly low level. 16 00:01:17,790 --> 00:01:23,580 All right so our code today is going to be made of two parts. 17 00:01:23,670 --> 00:01:27,380 So we have a server and a client. 18 00:01:28,200 --> 00:01:34,590 The server is going to listen or wait for messages while the client is the one sending the messages. 19 00:01:34,590 --> 00:01:43,320 So we'll start by coding the server and we'll import import not server but import socket which is the 20 00:01:43,320 --> 00:01:46,980 module that we're using then we'll define the server address. 21 00:01:47,010 --> 00:01:53,700 So let's say hosts equals socket guest host name. 22 00:01:54,000 --> 00:01:54,750 Okay. 23 00:01:54,840 --> 00:02:02,970 Now this method connects to local hosts then will specify a port number we want to listen on. 24 00:02:02,970 --> 00:02:07,410 So we'll say fourth equals 9 3 3 7. 25 00:02:07,410 --> 00:02:09,780 Just a random port number. 26 00:02:09,960 --> 00:02:16,720 Now notice the host the host is going to be a string while the port in this case is an integer. 27 00:02:16,740 --> 00:02:21,750 And this is all the socket module wants us to specify the address details. 28 00:02:22,180 --> 00:02:22,670 Okay. 29 00:02:22,680 --> 00:02:29,940 And then what we'll do is to create a socket object so let's say SOC on their sport equals socket light 30 00:02:30,720 --> 00:02:33,750 socket. 31 00:02:33,750 --> 00:02:44,190 And we're going to use socket f address family iiNet Internet and the type of socket is going to be 32 00:02:44,190 --> 00:02:50,580 socket SOC under sports or soft underscored stream. 33 00:02:51,120 --> 00:02:55,550 So this is for DCP communication. 34 00:02:55,650 --> 00:03:05,460 Things like I said here f underscore iiNet is the address family for IP V for and soc underscore stream 35 00:03:05,580 --> 00:03:11,490 specifies that we're going for a DCP connection and there's also SOC underscored that diagram which 36 00:03:11,490 --> 00:03:16,410 is for you the IP often faster and less reliable. 37 00:03:16,710 --> 00:03:17,360 Next. 38 00:03:17,490 --> 00:03:18,380 We've been the socket. 39 00:03:18,390 --> 00:03:23,730 We've just created to the host and the port variables that we've just defined so we do that with soc 40 00:03:23,820 --> 00:03:29,160 underscored that then and we're going to say host port. 41 00:03:30,120 --> 00:03:32,640 And as you can see it's taken as a tuple. 42 00:03:32,730 --> 00:03:38,070 Now we could be more flexible here by allowing the port than the host to be given in the command line 43 00:03:38,460 --> 00:03:45,960 and in that case we'd be using this as module and maybe we'll do that and another more advanced class. 44 00:03:46,320 --> 00:03:57,000 OK so we've got a server defined now we need to actually have that less for incoming connections. 45 00:03:57,000 --> 00:04:00,290 So we do that with soc underscore that. 46 00:04:00,460 --> 00:04:08,330 Listen one one here denotes the backlog or the number of unaccepted connections before this server starts 47 00:04:08,340 --> 00:04:10,290 refusing new connections. 48 00:04:10,290 --> 00:04:18,750 Now let's also have something printed to the output so we'll say print on a new line server started. 49 00:04:18,750 --> 00:04:24,990 Dot dot dot and then we'll go to a new line then we need to start accepting connections using the DOT 50 00:04:25,020 --> 00:04:26,570 accept method. 51 00:04:26,640 --> 00:04:36,990 So it will say Con a DDR equals SOC underscore dot accept. 52 00:04:37,860 --> 00:04:44,310 Meaning that we accept connections from the client socket con and it's address a DDR once connection 53 00:04:44,310 --> 00:04:55,800 is established and then we'll do another print saying connection established with 54 00:04:58,460 --> 00:05:05,330 and then I'd say string a DDR then we'll send a message back to the client. 55 00:05:05,330 --> 00:05:13,970 So first let's actually put this message into a variable so we'll say message equals on a new line. 56 00:05:13,970 --> 00:05:25,430 Thank you for connecting not connection with connecting space and then plus string of a DDR and then 57 00:05:25,430 --> 00:05:29,900 we're actually going to send this message with a coin that send 58 00:05:32,810 --> 00:05:41,690 we send the message dot in codes or we need to specify the encoding because we're in Python 3 will encoded 59 00:05:42,230 --> 00:05:46,970 in ASCII and then close the socket. 60 00:05:46,970 --> 00:05:52,470 So con on that close. 61 00:05:52,520 --> 00:05:59,060 OK so when we run the script is going to start listening for connections on Port 9 3 3 7 and whoever 62 00:05:59,060 --> 00:06:06,270 connects it's going to send the message to and then immediately close the socket. 63 00:06:06,320 --> 00:06:11,860 We could also run this in a while loop to listen continuously but will not do that here. 64 00:06:11,890 --> 00:06:12,800 All right. 65 00:06:12,830 --> 00:06:15,320 Next we'll go to. 66 00:06:15,350 --> 00:06:22,910 But first let's actually save this and then we'll go to the client and call the decline side of it which 67 00:06:22,910 --> 00:06:26,120 is relatively easier. 68 00:06:26,300 --> 00:06:31,750 So we start with importing the socket module and then create a socket. 69 00:06:31,850 --> 00:06:39,110 So SOC under sport socket that socket exactly the same thing as previously. 70 00:06:39,830 --> 00:06:44,150 So socket dot. 71 00:06:44,860 --> 00:06:47,540 Am I not. 72 00:06:47,640 --> 00:06:48,570 Okay. 73 00:06:48,710 --> 00:06:54,970 And the type of socket is going to be SOC underscore stream. 74 00:06:55,220 --> 00:07:00,290 Then we can simply use the Connect method to connect to the server without wasting time defining variables 75 00:07:00,290 --> 00:07:01,860 for the host and port. 76 00:07:01,930 --> 00:07:04,440 Like with that in the server dot P Y. 77 00:07:04,670 --> 00:07:10,910 Again like I said you can code the script such as in a way that it receives these values via the command 78 00:07:10,910 --> 00:07:11,390 line. 79 00:07:11,390 --> 00:07:17,840 So anyway in this case will connect to the server using SOC underscored that connect 80 00:07:20,600 --> 00:07:25,410 and we simply pass on socket dot get post to name. 81 00:07:25,480 --> 00:07:26,940 OK. 82 00:07:27,020 --> 00:07:32,300 And then also the port 9 3 3 7 as a tuple. 83 00:07:32,300 --> 00:07:37,940 So once connection is established we want to be able to receive whatever message the server sends. 84 00:07:37,940 --> 00:07:50,060 So we define a variable message equals SOC underscore dot e CV V and how many bytes we want to receive 85 00:07:50,380 --> 00:07:52,350 10 twenty. 86 00:07:52,820 --> 00:08:00,290 Once that's done we'll close the socket with the SOC underscored our clothes and also print the message 87 00:08:00,290 --> 00:08:05,150 we got from the server with print and as G the decode. 88 00:08:05,330 --> 00:08:09,140 Of course ASCII control s and that's it. 89 00:08:09,140 --> 00:08:13,320 A very basic DCP client server coded in python. 90 00:08:13,340 --> 00:08:14,840 Now let's actually put it to work. 91 00:08:15,170 --> 00:08:20,990 So I'm going to open up a command prompt and navigate to desktop where both the client and the server 92 00:08:20,990 --> 00:08:30,290 are located and we'll start the server with Python server dot P Why says server started and then open 93 00:08:30,290 --> 00:08:36,350 up a another command prompt put them side by side so that you can actually see how they work 94 00:08:40,950 --> 00:08:46,140 so the server is listening and I also have to navigate to the desktop here. 95 00:08:46,140 --> 00:08:52,340 And when I hit Python client thought P Y you're going to see how it connects to the server. 96 00:08:52,350 --> 00:08:57,460 Send the message and we basically can't see the communication between the two of them. 97 00:08:57,480 --> 00:08:59,400 So I'm going to hit enter. 98 00:08:59,490 --> 00:09:03,550 Okay so we have a little error here is coquette. 99 00:09:03,810 --> 00:09:05,710 So it's over here. 100 00:09:05,940 --> 00:09:14,290 So they have socket control s then we go back go back over here and then do that one more time. 101 00:09:14,670 --> 00:09:20,070 And as you can see the connection took place so when the client connected its connection established 102 00:09:20,100 --> 00:09:24,870 with the IP and then it sent the message and it sent. 103 00:09:24,870 --> 00:09:26,880 Thank you for connecting. 104 00:09:27,840 --> 00:09:28,590 Awesome right. 105 00:09:28,590 --> 00:09:33,060 So this was a pretty basic networking Doc through the socket module. 106 00:09:33,120 --> 00:09:40,080 Of course you can further expand these codes by for example allowing ongoing lesson on the server side 107 00:09:40,080 --> 00:09:46,680 using loops by receiving command line arguments by executing commands on the server from the client 108 00:09:47,040 --> 00:09:52,590 by messaging back and forth between the server and the client like a basic chat application for example 109 00:09:52,980 --> 00:09:56,400 by encrypting the communication and much much more. 110 00:09:56,460 --> 00:10:01,890 So it's like I always say the only limit is your imagination.