Write a TCP Server in Python – Part 2

This article is for people that have good knowledge in Python and want to learn and practise new skills and do some network basics. Information about sockets is very important when using Python for network scripting. Before we can create and use sockets we have to import the socket library as we explained and showed in Part 1 of this series.

If you have been introduced to my articles about basics in Python you know that we have worked in interactive mode, but now is time to learn and create scripts. To create scripts, we need a text editor and after we finish writing the code we save the file with the extension .py which stands for Python. The text editor I will use is called vim, which is almost compatible version of the Unix editor vi and I think that it’s a well known text editor to Linux geeks. I must say I love this text editor because it offers a beautiful syntax highlighting that makes my code easy to read and friendly to other people.

The first line we will write is called the shebang line which indicates what interpreter to use. This line is necessary only in Unix base platforms such as Linux, Mac OS and does not apply to other platforms. We will use Python interpreter to interpret our Python scripts so the shebang line will be #!/usr/bin/Python like shown in Figure1. If you have problems with vim and never used it before feel free to post you question in the comment section or you can use a text editor of your choice.

Creating the shebang line

Figure 1

After we finish creating the shebang line we import the socket library like shown in Figure 2.

importing the socket library

Figure 2

Now we can create a socket object. The variable I will use is called my_socket. How does the client identify the server and connects to it? The server has an IP address like houses have addresses. Since we will run our script in our local machine , the IP address will be 127.0.0.1 aka localhost. Our server should listen on some port, so we need a port number too.

Figure 3 shows how to create the socket object which we assign to my_socket variable and two other variables, one for the hostname of the server, one for the port in which the server will listen for connections.

Figure 3

Time to bind and listen, to do that we use the bind method and listen method like shown in Figure 4. The bind() method accepts a tuple with two elements, the server name and the port on which the server will listen for incoming connections. We know that when used inside expressions variables return their actual values so we use use our variables, hostname and port_number as elements for the tuple in bind() method. The listen() method sets up and start TCP listener.

Figure 4

We want our server to listen all the time, day and night. How do we do that in Python? To do that we need a condition that returns always true bolean value when tested. Open a new terminal in your Linux machine, type Python (interactive mode) and write the code exactly as shown in Figure 5.

Figure 5

Now press enter and see what happens (Figure 6). This simple program prints ‘unixmen’ string infinitly. This is what we need for the server. We will use the while conditional statement because it returns the ‘true’ bolean value all the time. Our server will accept client requests all the time so we need a while statement that is always true. Figure 7 shows the finished server and there are explanation comments for each line

Figure 6

Figure 7

The line ‘connection, address = my_socket.accept()’ accepts TCP client connection and waits until it arrives. The send() method is used to transmit TCP message. This is enough for a beginner in network scripting. You will learn about the client in the next articles, “How to program” and “How to connect to the server”.