Building a Simple Server-Client Database#
This Python script demonstrates a simple server-client model for querying an animal database. The server stores a list of animals along with their names and ages, and the client can request the names of animals from the server. The communication between the server and client is done using UDP sockets.
You can view the code for this here:
Server-Side Implementation#
The server maintains a dictionary of animals, where each key is an animal type and the value is a tuple containing names and ages of the animals. The server listens for incoming requests and responds with the requested animal data.
Key Components:
Animal Dictionary: A dictionary named our_animals stores the data of various animals.
Server Configuration: The server is configured to run on 127.0.0.1 (localhost) and port 53.
Socket Setup: A UDP socket is created and bound to the specified IP address and port.
Request Handling: The server continuously listens for incoming requests. When a request is received, it looks up the requested animal in the dictionary and sends the corresponding data back to the client. If the animal is not found, it sends a “not found” message.
import socket
# Animal Dictionary
our_animals = {
"lion": ("Bruce, 25yrs old", "Tony, 10yrs old", "Michael, 12 yrs old"),
"elephant": ("Ellie 108yrs old", "Ella, 30yrs old", "Dumbo 68yrs old"),
"giraffe": ("Gerry, 7yrs old", "Grace, 9yrs old", "Geoffrey, 14yrs old"),
"zebra": ("Stripey, 5yrs old", "Loki, 8yrs old", "Stella, 6yrs old"),
"monkey": ("Squiggles, 3yrs old", "Winston, 5yrs old", "Jiggles, 4yrs old"),
"emu": ("Karen, 15yrs old", "Judy, 12yrs old", "Ken, 18yrs old"),
"kangaroo": ("Kenny, 6yrs old", "Kylie, 4yrs old", "Kevin, 8yrs old"),
"penguin": ("Penny, 2yrs old", "Pablo, 3yrs old", "Pippin, 1yr old"),
"tiger": ("Tanya, 9yrs old", "Tyler, 6yrs old", "Tara, 7yrs old")
}
# Server IP and Port Number
server_ip = "127.0.0.1"
server_port = 53
# Socket set up for communications
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind((server_ip, server_port))
# Server running message
print("Our animal database is running...")
while True:
data, client_address = server_socket.recvfrom(1024)
animal = data.decode().strip().lower()
# If loop for communicating animals to server
if animal in our_animals:
response = " , ".join(our_animals[animal])
print("Client sent request for:", animal)
print("Server sent animal names:", response)
server_socket.sendto(response.encode(), client_address)
else:
server_socket.sendto("Animal not found in our database".encode(),client_address)
Client-Side Implementation#
The client allows the user to input an animal type and sends this request to the server. The client then receives and displays the server’s response. The user can continue to make requests until they choose to exit.
Key Components:
Server Configuration: The client is configured to connect to the server running on
127.0.0.1
(localhost) and port53
.Socket Setup: A UDP socket is created for communication with the server.
User Interaction: The client prompts the user to enter an animal type, sends the request to the server, and displays the received response. The user can continue querying the server until they choose to exit.
import socket
# Server IP and Port Number
server_ip = "127.0.0.1"
server_port = 53
# Socket set up for communications
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
animal = input("Enter an animal to find their names:")
client_socket.sendto(animal.encode(), (server_ip, server_port))
response, _ = client_socket.recvfrom(1024)
print("The names for those animals in our database are:", response.decode())
choice = input("Do you want to continue? (y/n")
if choice.lower() != "y":
break
client_socket.close()
How to run the code#
In order to run this code, you need to open both files up in pycharm or vscodes and run the server first and then the client. The server will be running in the background and the client will be sending requests to the server. Then you will interact with the client side where you will enter an animal type, and if the animal type is in the server’s database, it will return the name and age of the animal. If the animal type is not in the database, it will return “Animal not found”.
How the output will look:#
Here is a sample of the output from running the code, as you can see the table is updated each time you provide an IP address and the MAC address is found. Or if the IP address isn’t found, it repsonds with not found
and the table remains the same.
Client Output
Enter an animal to find their names:lion
The names for those animals in our database are: Bruce, 25yrs old , Tony, 10yrs old , Michael, 12 yrs old
Do you want to continue? (y/ny
Enter an animal to find their names:elephant
The names for those animals in our database are: Ellie 108yrs old , Ella, 30yrs old , Dumbo 68yrs old
Do you want to continue? (y/ny
Enter an animal to find their names:tigger
The names for those animals in our database are: Animal not found in our database
Do you want to continue? (y/ny
Enter an animal to find their names:tiger
The names for those animals in our database are: Tanya, 9yrs old , Tyler, 6yrs old , Tara, 7yrs old
Do you want to continue? (y/nn
Process finished with exit code 0