In this tutorial, we will be building a simple server-client Python program that allows the client to send a request for an animal's name, and the server responds with the names of animals of that type from a predefined dictionary.
- Basic knowledge of Python programming.
- Understanding of sockets and basic networking concepts.
-
Open the provided
server.pyfile. -
You will notice that we import the
socketmodule. This module provides us with the necessary functions for creating and managing sockets. -
Define the IP address and port number on which the server will listen for incoming requests. Update the
server_ipandserver_portvariables with the appropriate values. -
Create a socket for communication using
socket.socket(socket.AF_INET, socket.SOCK_DGRAM). Here,AF_INETindicates the use of IPv4 andSOCK_DGRAMindicates that we are using a UDP socket for communication. -
Bind the socket to the server IP and port using
server_socket.bind((server_ip, server_port)). -
A while loop has been implemented to keep the server running indefinitely. Inside the loop:
- Receive data and client address using
data, client_address = server_socket.recvfrom(1024). - Convert the received data (animal name) to lowercase and strip any whitespace.
- Check if the received animal name exists in the
our_animalsdictionary. If it exists, prepare the response by joining the animal names and send it back to the client usingserver_socket.sendto(response.encode(), client_address). If the animal name is not found, send an appropriate error message.
- Receive data and client address using
-
Close the server socket using
server_socket.close().
-
Open the provided
client.pyfile. -
Similar to the server side, import the
socketmodule. -
Define the server IP and port number in the
server_ipandserver_portvariables. -
Create a socket for communication using
socket.socket(socket.AF_INET, socket.SOCK_DGRAM). -
Enter a
while Trueloop to keep the client running until the user decides to exit. -
Inside the loop, prompt the user to enter an animal name using
input(). Send the entered animal name to the server usingclient_socket.sendto(animal.encode(), (server_ip, server_port)). -
Receive the response from the server using
response, _ = client_socket.recvfrom(1024)and print the names of animals associated with the provided animal type. -
Ask the user if they want to continue. If the input is not 'y', break out of the loop.
-
Close the client socket using
client_socket.close().
-
Run the
server.pyfile to start the animal database server. -
Run the
client.pyfile to start the client. -
The client will prompt you to enter an animal type. For example, you can enter "lion".
-
The server will respond with the names and ages of lions from the predefined
our_animalsdictionary. -
The client will then ask if you want to continue. If you enter 'y', you can request information for another animal. If you enter anything else, the client will exit.
In this tutorial, we've created a basic server-client program to fetch animal names from a server's predefined dictionary. This example serves as a simple introduction to socket programming in Python and can be expanded upon for more complex applications. Remember to consider error handling, security, and scalability when working with real-world networking scenarios.