Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Making an Echo Client

00:00 In this lesson, you’ll learn about the client that will connect with the echo server example. The client begins with phase two of the process, where it creates a socket to connect with the server, and those instructions are much simpler than the server’s.

00:15 Let’s write this program again. You want to import the socket module,

00:25 hard-coding the values for the server this client will connect to.

00:34 Creating the socket, using the same constants for the initializer, version four address, and TCP sockets.

00:45 And then finally, a call to connect to the desired host and port. Once again, the program will pause here until the connection is accepted and established.

00:55 Again, creating a socket in the same way as the server socket, then making a connection using the server’s HOST and PORT values.

01:05 Once the connection is established, it’s time to exchange data. Let’s add this to the client program. The client starts the process by sending data to the server.

01:16 This data usually includes information about what resource of the server it would like, but in this example, the request is simply data that will be echoed back identically. Here, the familiar message "Hello, world".

01:29 The b preceding the string means that this data will be sent in 8-bit units. The client then waits to receive whatever data the sender will send back, and that will be stored in the variable data.

01:43 There’s no more use for the server, so in the code, you end this with block, automatically closing the socket. Finally, the client will display on its console the message that was echoed back.

01:57 First, the client sends the data to the server, then attempts to receive the server’s data back. Then, having no other use for the server, this block of code ends, and the connection is closed, and the client then displays the data it received.

02:16 In the next lesson, you’ll see these programs in action.

Become a Member to join the conversation.