Create a server client network with python socket module

In this python networking example We will create the client and server program with the socket module and make them communicate with each other. The client will send two numbers in a UDP datagram to a server and the server returns a packet of data containing the total of that two numbers. In order for the communication to successes the server needs to listen at a particular address and port and the client needs to know these two values in order to deliver its message and receive the answer from the server.

In the following client and server code, address is in a tuple of (address, port). The address will consist of a string which can be a name or an IP address. When your programs are just talking to one another on the same machine, you can use the name ‘localhost’ or the equivalent address ‘127.0.0.1’. You can select the port which normally will range from 6000 to 6999. Since this is just an example any port will do. For more information about the port visit this site to learn more.

Now let’s create two program, the first program is the client which will ask for the sum of two values by sending those values to the server and the second is the server which will return the answer to the client. In each
program, we’ll print the time and open a socket. The server will listen for connections to its socket, and the client will write to its socket, which transmits a message to the server.

The first program is the server.

from datetime import datetime
import socket

server_address = ('localhost', 6888)
max_size = 4096

print('Starting the server at', datetime.now())
print('Waiting for a client to call.')

server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind(server_address)
data, client = server.recvfrom(max_size)

print('At', datetime.now(), client, ' wants to know the sum of these two numbers ', data)

num1, num2 = data.decode("utf-8").split(',')
total = int(num1) + int(num2)
sum = "The total sum is " + str(total)

server.sendto(sum.encode(), client)
server.close()

We will start the server first, open a command prompt window and start that python file.

server side
server side

Next we will create a client side program.

import socket
from datetime import datetime

server_address = ('localhost', 6888)
max_size = 4096

print('Starting the client at', datetime.now())
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.sendto(b'20,60', server_address)

data, server = client.recvfrom(max_size)
print('At', datetime.now(), server, 'said', data)
client.close()

Now start the program in a new command prompt window and you will get the answer to your question. As you can see the computer will automatically assign the ip address and the port number to the client side so we do not need to do anything with it.

client side
client side

UDP sends data in single chunks and it does not guarantee delivery but since this is just an example and we are sending the message only to our local computer then it is alright!

Leave a Reply

Be the First to Comment!

avatar
  Subscribe  
Notify of