DESCRIPTION: In this assignment, you will have a chance to try your hand with th
ID: 3731120 • Letter: D
Question
DESCRIPTION: In this assignment, you will have a chance to try your hand with the Python programming language. Your job will be to develop a small database server. Ordinarily, this would be a significant undertaking. However, with Python, this will be surprisingly straightforward and requires a relatively modest amount of code. Your DB system will work as follows: 1. You will construct a client/server application. In your case, only one client program will access the DB, so you do not have to worry about issues like concurrency or thread control It's just a one-to-one form of communication. Python provides a SocketServer class in its standard libraries. You can use this to get started, along with the sample code provided in the Python docs (you can use the 9999 port for the DB server). In addition to listening for client requests, the server program must first load the database and provide access to the data. The data base will be loaded from a simple, plain text disk file called data.txt. In your case, the database will hold customer records. A customer record will be a tuple with the following format 2. name, age, address, phone# To record this informatio n on disk, you will store one record per line, and separate each field with a bar symbol CT). A simple 3 record database might look like this John 43 123 Apple street|514 428-3452 Katyal 26149 Queen Mary Road1514 234-7654 Ahmadl91|1888 Pepper LanelExplanation / Answer
I'm explaining the concept for Finding the customer along with the code. This could be implemented similary for the other requirements. The Explanation for other functionalities has also been given at the end.
Socket class can be implemented on both server (server.py) and client (client.py). Establishing the connection has been given in the code along with the comments. While executing this, the file data.txt has to be created with sample data and put in the same folder as server.py, since server.py first loads the contents of the file to an array. Alternatively any datastructure list,tuple etc., could be used too.
Client has been given a choice to choose the operation first. This would be sent to server where the bytes have been converted to String, checked against the type of operation. Suppose find the customer has been selected at the client instance, Server then asks for the name of the customer. As the name of the customer is sent by the client it would be matched against the array elements one after the other. Since name has to be matched with the string before the first delimiter the same parsing has to be done while comparing. The result could be sent to client.
---------------------------------------------------------------------------------------------------------------------------------
Server.py
# This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
with open("file.txt", "r") as ins:
array = []
for line in ins:
array.append(line)
array.split('|')[1]
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
choice=s.recv(10240) # This will be bytes
choiceString=choice.decode('utf-8') #converting bytes to string
if choiceString == "1" :
c.send('Name of the Customer, Please')
cust = s.recv(10240) # This will be bytes
custString=choice.decode('utf-8')
for i in array
if array[i].split('|')[1]==custString :
c.send(array[i])
break
else :
c.send('The Customer is not found');
# Similarly write for other requirements
c.close() # Close the connection
---------------------------------------------------------------------------------------------------------------------------------
Client.py
# This is a client Program
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.connect((host, port))
print "Select the Operation that you want to perform"
print " 1.Find Customer 2.Add Customer "
sel = input("Enter your choice: ")
s.send(sel)
print s.recv(10240) # Prints Name of the Customer, Please
cust = sel = input("Enter the Name: ")
s.send(cust)
print s.recv(10240)
s.close # Close the socket when done
----------------------------------------------------------------------------------------------------------------------------------
Similary, other requirements could be handled too. When it comes to adding a customer : all the details have to be individually inputted at the client, concatenated with the pipe and send to server. Convert it to string, add to array and append to the file. Deleting a name is similar to adding here we will have to input only name at the client end and use the same delimiter comparision as done while finding and then delete from array and the file. Hope you get a gist of how the other could be handled.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.