Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

I need to modify the below code to send a string from client to server, it needs to ask the server to send back the

I need to modify the below code to send a string from client to server, it needs to ask the server to send back the reverse string to the client. For example 1. Client send "12345" to server 2. Server send "54321" back to client. It needs to be written in Python. Thank you!

image text in transcribed

editable code:

#import socket module from socket import * import sys # In order to terminate the program serverSocket = socket(AF_INET, SOCK_STREAM) #Prepare a sever socket #Fill in start serverHost = '192.168.0.103' serverPort = 6789 serverSocket.bind((serverHost,serverPort)) serverSocket.listen(5) #Fill in end while True: #Establish the connection print('Ready to serve...') connectionSocket, addr = serverSocket.accept() #Fill in start #Fill in end

try: message = connectionSocket.recv(4096) #Fill in start #Fill in end filename = message.split()[1] f = open(filename[1:]) outputdata = f.readlines() #Fill in start #Fill in end #Send one HTTP header line into socket #Fill in start connectionSocket.Send("HTTP/1.1 200 OK Content-Type: text/html ") connectionSocket.Send(" ") #Fill in end #Send the content of the requested file to the client for i in range(0, len(outputdata)): connectionSocket.send(outputdata[i].encode()) connectionSocket.send(" ".encode())

connectionSocket.close() except IOError: #Send response message for file not found #Fill in start connectionSocket.send("HTTP/ 1.1 404 Not Found ") connectionSocket.send("Content-Type: text/html ") connectionSocket.send(" ") connectionSocket.send("

404 Not Found

") #Fill in end #Close client socket #Fill in start connectionSocket.close() #Fill in end serverSocket.close() sys.exit()#Terminate the program after sending the corresponding data

#import socket module from socket import * import sys # In order to terminate the program serverSocket socket (AF_INET, SOCK_STREAM) #Prepare a sever socket #Fill in start serverHost192.168.0.103 serverPort 6789 serverSocket.bind ((serverHost, serverPort)) serverSocket.listen(5) #Fill in end while True: # stablish the connection print('Ready to serve...) connectionSocket, addr-: serverSocket.accept() #Fill in start #Fill in end try: #Fill in start #Fill in end message-connectionSocket.recy(4096) filename-message.split)[1] f - open(filename[1:]) outputdata -f.readlines() #Fill in start #send one HTTP header line into socket #Fill in end #Fill in start connectionSocket.Send("HTTP/1.1 200 OK Content-Type: text/html ") connectionSocket.send("r ") #Fill in end #Send the content of the requested file to the client for i in range(e, len (outputdata)): connectionSocket.send (outputdata[i].encode()) connectionSocket.send("r ".encode()) connectionSocket.close) except IOError: #send response message for file not found #Fill in start connectionSocket.send("HTTP/ 1.1 484 Not Found ") connectionSocket.send("Content-Type: text/html ") connectionSocket.send("r ") connectionSocket.send("xhead>

484 Not Found

x/html> ") #Fill in end #Close client socket #Fill in start connectionSocket.close) #Fill in end serverSocket.close() sys.exit()#Terminate the program after sending the corresponding data

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions