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 Not Found Message # client py
import socket
import sys
def httpclientserverHostserverPort,filename:
clientsocketsocket.socketsocketAFINET,socket.SOCKSTREAM
try:
#Connect to server
clientsocket.connectserverHostserverPort
#send HTTP request
requestfGET filename HTTPr
Host: serverHostr
r
clientsocket.sendallrequestencode
#display server response
response clientsocket.recvdecode
printresponse
except socket.error as e:
printfError: e
finally:
clientsocket.close
if lensysargv :
printclientpy serverHost serverPort filename
else:
serverHostsysargv
serverPortintsysargv
filenamesysargv
httpclientserverHostserverPort,filename #server.pyfrom socket import
import sys # In order to terminate the program
serverSocket socketAFINET, SOCKSTREAM
#Prepare a sever socket
serverPort #port number
serverSocket.bindserverPort #bind socket to host IP and port number
serverSocket.listen #listen for incoming connectionsone request at a time
while True:
#Establish the connection
printReady to serve...
connectionSocket, addr serverSocket.accept
try: #Receive HTTP request message
message connectionSocket.recvdecode
filename message.split
#Open file
try:
with openfilename:rb as f :
outputdata fread
#Send one HTTP header line into socket
connectionSocket.sendHTTP OKr
r
encode
#Send the ontent of the requested file to the client
for i in range lenoutputdata:
connectionSocket.sendoutputdatai:i
#Send response message for file not found
except FileNotFoundError:
connectionSocket.sendHTTP Not Foundr
r
encode
except Exception as e:
printfError:e
#Close client socket
finally:
connectionSocket.close
serverSocket.close
#Terminate the program after sending the corresponding data
sysexit
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
