*Please write a python server and client code using the following protocol* The
ID: 3886877 • Letter: #
Question
*Please write a python server and client code using the following protocol*
The server stores a list of received messages and saves them so they can be requested to be sent back to the client at any time by list entry. Messages are ordered by the date they are received and then by number. For example, all diary entries are stored in numerical consecutive order. Message entries will be indicated by the command /a or /A (case insensitive) for Add message followed a space. Diary entries will be entered as single line messages and inside of quotations marks. Message entries marked for removal would utilize /r or /R.
Explanation / Answer
#!/usr/bin/python
#importing library for sockets
import socket
#importing library for getting date and time
from datetime import datetime as dt
#importing library for executing system commands
import commands as cmd
#creating socket
s=socket.socket()
#getting the hostname
host=socket.gethostname()
port=12345
#binding the socket with the host
s.bind((host,port))
#listening to the socket
s.listen(5)
#accepting the connection
c,addr=s.accept()
while True:
#message recieved from the client
message = c.recv(1024)
#getting the first two characters of the message
first_2_chars = message[0:2]
#if first two charaters are /a
if first_2_chars == "/a":
#open the log file
FILE=open("Log","a")
#get todays date and time
date = str(dt.today())
#Write the data to the Log file
#:,: is used as a delimiter
FILE.write(message[2:]+":,:"+date+" ")
FILE.close()
#sending acknowledgement to the user
c.send('Your Message has been recieved successfully')
#if first two characters are /r
elif first_2_chars == "/r":
#creating a temp file
temp=open("temp","w")
#open the log file in read mode
FILE=open("Log","r")
for x in FILE:
#matching the recieved mesasge with the messages in log file
if x.split(":,:")[0]!=message[2:]:
temp.write(x)
temp.close()
FILE.close()
#remove the current log file
cmd.getstatus("echo 'y' | rm Log")
#rename the temp file as the new Log file
cmd.getstatus("mv temp Log")
c.send("Your message was successfully deleted")
Client.py
#!/usr/bin/python
import socket
#creating socket
s=socket.socket()
#getting hostname
host=socket.gethostname()
port=12345
#establishing socket
s.connect((host,port))
while True:
#menu for the user
print "1 : Add a message to the log"
print "2 : Delete a message from the log"
print "3 : Exit"
#to accept the user's choice
ch=raw_input("Enter your choice here : ")
if ch == "1":
#user enter's message he wants to add
message=raw_input("Enter a message to share : ")
s.send("/a"+message)
elif ch == "2":
#user enter's message he wants to delete
message=raw_input("Enter a message to delete : ")
s.send("/r"+message)
elif ch == "3":
break
else:
print "Invalid choice."
continue
#To print the acknowledgement from the user
print s.recv(1024)
s.close()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.