Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:48Length:7.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

8.5 Create a Socket Client

In the previous lesson, we successfully created a simple socket server that we could connect to via telnet. But the server is only half of the puzzle. We also need to create a client to interact with that server! In this lesson, we are going to do just that.

1.Introduction
2 lessons, 11:32

1.1
Introduction
02:03

1.2
Prerequisites
09:29

2.Python Building Blocks
6 lessons, 1:08:07

2.1
Introduction to the Interpreter
09:51

2.2
Numbers
10:53

2.3
Strings
14:36

2.4
Lists
11:33

2.5
Standard Input and Formatting
12:00

2.6
Building a Tip Calculator
09:14

3.Controlling the Flow
7 lessons, 1:20:10

3.1
Conditional Statements
12:47

3.2
Looping With For
09:36

3.3
The Range Function
10:41

3.4
Looping With While
13:06

3.5
Creating Functions: Part 1
11:55

3.6
Creating Functions: Part 2
08:49

3.7
Building an Average Calculator
13:16

4.Common Data Structures
4 lessons, 46:49

4.1
Lists, Stacks, and Queues, Oh My!
11:45

4.2
Dictionaries
10:10

4.3
Iterating Data Structures
09:41

4.4
Building a Sentence Analyzer
15:13

5.Application Structure
7 lessons, 1:15:12

5.1
Modules
09:08

5.2
Packages
11:24

5.3
Classes
09:53

5.4
Attributes
09:34

5.5
Methods
12:01

5.6
A Special Calculator: Part 1
13:36

5.7
A Special Calculator: Part 2
09:36

6.Collections
7 lessons, 46:55

6.1
What Are Comprehensions?
06:32

6.2
List Comprehensions
06:08

6.3
Dictionary Comprehensions
06:38

6.4
Map
05:45

6.5
Filter
06:31

6.6
Lambdas
05:21

6.7
Generators
10:00

7.File I/O
6 lessons, 48:51

7.1
File Basics
06:50

7.2
Reading Entire Files
07:49

7.3
Navigating a File
08:32

7.4
Writing to Files
07:22

7.5
Reading and Writing to Files
09:15

7.6
Reading and Writing Complex Objects
09:03

8.Networking
5 lessons, 43:48

8.1
Introducing the Socket
04:39

8.2
Getting a Remote IP Address
06:42

8.3
Handling Socket Errors
07:58

8.4
Create a Socket Server
16:04

8.5
Create a Socket Client
08:25

9.Connecting to Network Services
3 lessons, 34:27

9.1
Getting the Current Time With NTP
10:38

9.2
Getting Websites With HTTP
12:57

9.3
Downloading Files With FTP
10:52

10.Conclusion
1 lesson, 02:08

10.1
Goodbye
02:08


8.5 Create a Socket Client

Now that we have our server application set up and ready to go, it is nice to be able to test things out and connect to our socket server with telnet. But we also wanna be able to do things more programmatically from separate clients, if that's the type of application we wanna be doing it in. And in this particular example, that's where we wanna go. Now I want to get rid of that telnet. Take telnet out of the equation, and write a very simple client application that's going to connect to my server, it's gonna send it some data, and it's gonna receive some data back. So that's where we wanna get to by the end of this lesson. And you're gonna find that once you understand the basics of creating a server using sockets, creating a client is actually quite easy. And it's actually a lot less steps. You don't have to worry so much about listening and binding, and all that sort of stuff. In this case, we're simply gonna create a connection, we're gonna send data, and we're gonna receive data. All things that you learned when creating our server here. So I'm gonna go ahead and create a new file here. And I'm gonna save this, I'm gonna save it as 5-client.py, like that. So now what we wanna do is basically the same process that we followed before. I'm gonna import socket just like I did on my server. But in this case, we have a couple fewer steps. So the first thing that we wanna do again is create a new instance of a socket. And that's actually gonna be exactly the same as we did with the server. So I'm gonna say s = socket.socket, and I'm gonna pass in AF_INET. So I wanna make sure that I'm communicating on the same type of socket as far as the address families are concerned, as the server that I created. But I need to use socket.AF_INET. And then I'm gonna specify the connection type. And I want to mimic what I did on the server side, so I'm gonna say SOCK_STREAM, like that. And then, I am going to connect to my server. So I'm gonna skip the binding, I am going to skip the listening, I'm gonna go right to creating a connection. And once again, the terminology of what we're trying to achieve here mimics the functions on the socket. So I'm gonna say s.connect. And in this case, I'm send in the same pair that I was setting up on the binding on my server side, so this guy right here. I'm actually going to copy this. I'm gonna come over to my client, and I'm just gonna paste that in. So I wanna connect to IP address. My Loopback Adapter 127.0.0.1 on port 8000. So that's what I'm looking to connect to from my client. Once I've connected to my client, then I wanna send it some data. So that data could be anything, just like I did in the telnet example. I could say hello there or whatever. So in this case, I'm just going to say s.send. I'm going to say My name is Derrick, just like that. So really, once again, I'm sending data using the send function. Now remember, in our server application, the functionality that was there was I"m going to receive some data. Once I'm done receiving data, I'm going to print out what I received, and then I'm gonna send it back. I'm gonna echo it back. So on the client side, I also want to receive whatever's coming back from the server. So this is a connection based, I'm sending a request, and then I'm going to receive some data back. So once again, I'm going to say data = s.recv. I'm going to receive data from that server. And once again, I'm going to just do 1024 as a buffer size. So this is the same basic process that we did before where we're receiving data into a buffer, and we're gonna process that information. And this is gonna be fairly small, once again, so I'm just going to receive it one time. Then I wanna print it out. I'll say print 'Received from server', and I want to print out the data that I received. Just like that, so pretty simple stuff. And then once I'm all done with this, I just want to close the connection. I wanna make sure once again that I close that connection when I'm all done. So I'm going to say s.close. I wanna close my socket, and then I'll just simply print here. 'Client is disconnected', like that. Just so that we know everything has been closed out and disconnected and cleaned up, ready for use by someone else. So now that we've got this, we need to run both our client and our server. So let's make sure that everything is saved. We'll come back over and start up our server. So we'll do the same thing that we did before. We're listening for connections. Now I'm gonna open up another terminal once again. And this time, I'm going to run my client. I'm gonna hit Enter, and let's see what happened here. So I have initiated a connection. So we can see we're connected to from client 127.0.0.1 on port 60700. The next thing that it did was it sent data over. So we received from client My name is Derek, and then it sent data back to my server. And it says that I received from server My name is Derek, so it echoed everything back successfully. We see that it worked. And then it closed the connection. So we see that we jumped out of the connection on both the client and the server, and now my client is disconnected. So there you go. We now have the process of creating a basic server-application with low-level sockets using Python. And we can also connect to those low-level socket servers from Python as well. Now where can you go from here? My gosh, you could go everywhere. You could build any sort of networked application right now with knowing just this. Now obviously, you'll need to know some more Python constructs, but you could create web servers, you could create application servers, you could create chat applications. You could create all sorts of crazy things, FTP sites, email servers, you could do everything, but there's a problem. Just because you can do something with a socket, doesn't mean it's meant to be done. And what do I say that for? Well, yes, you could go off and run and creates the latest and greatest web server, better than Apache, better than IIS, all those types of things, and you could do it all using sockets. But it's a heck of a lot of work, and that's why people don't do those things on a regular basis. Once a product is out there and becomes heavily used, it kinda becomes a standard, and that's what you stick with. Because the people that wrote those applications spent a lot of time working on them and perfecting them and tweaking them here and there. So to write another one using sockets from scratch is pretty tough. That's why, say, writing the next version of Photoshop using assembly language, just because you could do it doesn't mean it's to be done. So the nice thing about this is that there are abstraction layers above sockets because sockets are pretty low level. There are a number of libraries and modules within Python that are building on top of sockets to handle other types of scenarios. So let's say FTP sites or email servers. Or maybe a good example that I'm gonna show you in a few minutes is the time servers where you can go out on the Internet and retrieve the exact time that you can sync up the time on your machine with, things like that. Those are all basic applications built at some level down underneath the covers using sockets that are communicating using a particular protocol. And those protocols then get wrapped up into these other libraries and modules so that you can use them in a very simple fashion. But understand that under the covers at some point, there are sockets. So it always pays to understand the types of things that you're dealing with, in this case sockets, and have a respect for them. But then also appreciate the higher level libraries and modules that I'm gonna show you in the next several lessons.

Back to the top