How to create a server and a client in python by using network sockets

The Python computer programming language offers a module which can be used to interact with the networking interface; this module is called socket. Having some experience with this module, I decided to share it through this article, with the main purpose of helping other Python geeks get familiar with networking.

Through this article, you are going to learn how to create a simple server and a client with the help of the Python’s socket module. Both the client and the server will be very simple, just to illustrate the power of the socket module in practice.

Before going any further with this article, make sure to create two scripts: client.py and server.py. Each one of them will store the code for scripting a client and a server wth the help of the socket module, which is offered by default in the Python computer programming language.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

What is a socket?

Based on my experience with sockets through the Python computer programming language, a socket is an endpoint of a bidirectional communications channel. Not only can sockets be implemented to communicate between processes on the same machine, but also to send and receive data through computers on different continents.

Enough theory, let’s put the Python’s socket module in practice.

Code the client.py

The script client.py will serve as a client, which will send a request to the TCP listening server. Once you have managed to open the script in editing mode, write the following line of Python code.

import socket #  it imports the socket module, which is offered by default

Now that we have managed to import the socket module in the client.py module, we have to create a new socket by making use of the Python code which is shown below.

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '127.0.0.1'
port = 13

Once you have managed to create a fresh socket and also have defined the host and the port which the client will connect to, implement the Python code which is shown below in your client.py script.

client.connect((host, port))

The above function, part of the Python’s socket module, takes as an input a tuple which contains the host and the port which the client is going to connect to. Once the connection is being made, the client needs to receive data from the server.

The Python code which will be used to receive data from the server is being shown below.

data = client.recv(1024)
print(data)  # prints the data

In the end, the script client.py will look like the one which is being shown below.

import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '127.0.0.1'
port = 13
client.connect((host, port))
data = client.recv(1024)
print(data)

Code the server.py module

The server.py module will store the code for the TCP listening server, a server which will wait for incoming connections from different clients.

Open the server.py module in editing mode and type the following piece of Python code with the main purpose of importing the socket module.

import socket

Once you have managed to import the socket module inside the server.py file, create a fresh socket like the one shown below.

server = socket.socket(socket.AF_STREAM, socket.SOCK_STREAM)

Then bind an address and a port to the server by making use of the Python code which is being shown below.

server.bind(('127.0.0.1', 13))

Now that you have managed to bind an address and a port to the server, make use of the following method to listen for incoming connections from different clients.

server.listen(5)

The TCP listener is being setup. Since the server.py should listen for incoming connections to infinite, we need a while True loop to achieve this task.

while True:
    connection, address = server.accept()
    print('Got connection from', address)
    connection.send('Welcome to the TCP listener')
    connection.close()  # close the connection

In the end, the server.py module should look like the one being shown below.

import socket
server = socket.socket(socket.AF_STREAM, socket.SOCK_STREAM)
server.bind(('127.0.0.1', 13))
server.listen(5)
while True:
    connection, address = server.accept()
    print('Got connection from', address)
    connection.send('Welcome to the TCP listener')
    connection.close()  # close the connection

Run the client and the server

We have finally managed to code two simple modules, a client and a server. It is time to run them and see the results.

Open two terminal windows inside the folder in which you have stored both the client.py and server.py modules. We need to run the server first, so we can start the TCP listener to wait for incoming connections.

In the first terminal window, start the server by making use of the command which is shown below.

python server.py

Once you have managed to start the server, run the client.py the same way we used for the server.py script.

python client.py

If the code of both modules get executed successfully, the client and the server will communicate data to each other. On the terminal window in which you executed the server module, the following output should come out.

('Got connection from', ('127.0.0.1', 51444))

On the other hand, in the terminal window in which you executed the client, the following output should come out.

Welcome to the TCP listener

Final thoughts

The Python’s socket module is a very useful one when it comes to scripting applications that make use of the network interface. Through this article you learned how to code two simple modules, a client and a server. Although not very complex scripts, the client.py and the server.py make a perfect case for illustrating the powerful features of the Python’s socket module.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *

*