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

Creating an Echo Server

00:00 In the next few lessons, you’ll learn how to use the basic socket API to create a simple connection between two processes. This first example will be an echo server and a client to use it, meaning that the server is just going to echo back to the client any messages it receives. We’ll start with the server side of the process.

00:24 So remember, the first phase of the process is to create a socket and have it listen for a request to connect. Let’s go ahead and write this part of the server program.

00:35 First, you want to import the socket module, which is part of Python’s standard library. You don’t need to do a pip install or anything to get it.

00:44 Phase one of the process is to set up the listening socket. Values for the host and port number are being hard-coded as constants for this example, for simplicity’s sake.

00:56 Future examples will obtain these values in a more realistic way. This number represents the localhost, a reference to the computer this is running on. This server will only accept connections from this computer.

01:10 If you use an empty string for HOST, the server will accept a connection from any computer. This indicates what port to listen to. You can select any number larger than 1023.

01:22 If you’re sharing a system with other users, you might want to check with your system administrator what numbers you’re allowed to use. Socket is a resource Python can manage, so you can use a with statement to open it, and you don’t need to use a close statement. This creates the listening socket.

01:41 The constants to the initializer indicate that the program is going to use version four Internet addresses and TCP socket streams.

01:52 As you can probably guess, this binds the socket you just created to the host and port you want to listen to. This, too, does exactly what it says. It’s going to listen for a request to the server from this socket.

02:08 When the program gets to this line, it will pause, waiting to accept a connection. This is called blocking. No other processing on this program will take place until a connection is accepted. When a connection is accepted, the client and server negotiate a new port to use for their interaction.

02:25 It returns a socket address pair where the new socket, here called conn, will interact with the address returned, named addr, which includes the port the socket will use.

02:42 Here again are the details of each step as the program was being written. Create the socket with a resource manager, binding the socket to the desired host and port, waiting for a connection, then accepting the connection and creating a new socket to handle the communication with the client at its address.

03:05 Phase two of the process is performed by the client, and you’ll see in the next lesson how that works. But once the connection is established, we’re up to phase three, exchanging data. From this server’s perspective, all it needs to do is read the data it receives, then send it back.

03:23 Let’s go ahead and add this to our program.

03:33 Open the new socket using resource management. So again, you won’t need an explicit close statement.

03:41 There’ll be a simple diagnostic message sent to the server’s console to see where this connection is coming from. This next activity will take place as long as the client is sending data.

03:54 The server will read up to one kilobyte of data from the client.

04:00 If the client didn’t send any data in that packet, then the server will end execution of the loop by breaking out of it. Otherwise, it will send all that data it received back to the client.

04:13 And again, this loops until the client closes the connection. So you open the new socket and then, in a loop, receive data from the socket, which the client will have sent, and then send all of that data back.

04:28 This part of the program will repeat until the client sends an empty packet, indicating that it’s closing the connection. That completes the server part of the process.

04:41 In the next lesson, you’ll see the client side of this process.

Become a Member to join the conversation.