Question: Please Help: Instead of using a browser, write your own HTTP client to test your server. Your client will connect to the server using a

Please Help: Instead of using a browser, write your own HTTP client to test your server. Your client will connect to the server using a TCP connection, send an HTTP request to the server, and display the server response as an output. You can assume that the HTTP request sent is a GET method. The client should take command line arguments specifying the server IP address or host name, the port at which the server is listening, and the path at which the requested object is stored at the server.
The following is an input command format to run the client. (The code is below. I am having trouble with this part when I run the client.py I am not getting the cotents of the html file just the 400 Not Found Message # client py
import socket
import sys
def http_client(serverHost,serverPort,filename):
client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
#Connect to server
client_socket.connect((serverHost,serverPort))
#send HTTP request
request=f"GET {filename} HTTP/1.1\r
Host: {serverHost}\r
\r
"
client_socket.sendall(request.encode())
#display server response
response= client_socket.recv(4096).decode()
print(response)
except socket.error as e:
print(f"Error: {e}")
finally:
client_socket.close()
if len(sys.argv)!=4 :
print("client.py serverHost serverPort filename ")
else:
serverHost=sys.argv[1]
serverPort=int(sys.argv[2])
filename=sys.argv[3]
http_client(serverHost,serverPort,filename) #server.pyfrom socket import *
import sys # In order to terminate the program
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
serverPort=1002 #port number
serverSocket.bind(('',serverPort)) #bind socket to host IP and port number
serverSocket.listen() #listen for incoming connections(one request at a time)
while True:
#Establish the connection
print('Ready to serve...')
connectionSocket, addr = serverSocket.accept()
try: #Receive HTTP request message
message = connectionSocket.recv(1024).decode()
filename = message.split()[1]
#Open file
try:
with open(filename[1:],'rb') as f :
outputdata = f.read()
#Send one HTTP header line into socket
connectionSocket.send("HTTP/1.1200 OK\r
\r
".encode())
#Send the ontent of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i:i+1])
#Send response message for file not found
except FileNotFoundError:
connectionSocket.send("HTTP/1.1400 Not Found\r
\r
".encode())
except Exception as e:
print(f"Error:{e}")
#Close client socket
finally:
connectionSocket.close()
serverSocket.close()
#Terminate the program after sending the corresponding data
sys.exit()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!