Question: In Python: 1) Guess the mime type and use the result to form the content type so you can send different types of files back

In Python:

1) Guess the mime type and use the result to form the content type so you can send different types of files back to the client.

2) Make the section of code, after the accept() into a function and pass in the socket.

3) make the function into a thread.

CODE:

from socket import * def main(): serverPort = 8080 serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind(('localhost',serverPort)) serverSocket.listen(0) # number of backlogged connections print('server ready') while 1: try: connectionSocket,addr = serverSocket.accept() except IOError: print("Server Socket Accept Error") try: request = connectionSocket.recv(1024).decode('utf-8') print(request) except IOError: print("Server Socket Recv Error") if request: # https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html try: [Method,Request_URI,HHTP_Version] = request.split(' ',2) print(Method) print(Request_URI) print(HHTP_Version) except ValueError: print("Request Parse Error:" + request) try: # https://www.ietf.org/rfc/rfc2396.txt [scheme,hier_part]=Request_URI.split(":",1) print(scheme) print(hier_part) except ValueError: print("No Scheme") scheme = None hier_part = Request_URI # more parsing is required but assuming the Request_URI is a path print("Request URI is: "+hier_part) # see if the file is present if hier_part != "/": try: print("Request File is: "+hier_part) fo = open('.'+hier_part,"rb") except IOError: # here need to send a 404 error http_status = 'HTTP/1.1 404 Not Found ' http_content = 'Content-Type: text/html charset=utf-8 ' outputdata = 'Bad File' else: # right now only file we have is the icon outputdata = fo.read() fo.close() http_status = 'HTTP/1.1 200 OK ' http_content = 'Content-Type: image/x-icon ' else: # here we would the contents of index.html outputdata = '' \ +' test 

Index File

Should be index

' http_status = 'HTTP/1.1 200 OK ' http_content = 'Content-Type: text/html charset=utf-8 ' # send the response header connectionSocket.send(http_status.encode('utf-8')) connectionSocket.send('Connection: close '.encode('utf-8')) # https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html Should LengthString = 'Content-Length: '+str(len(outputdata))+' ' #connectionSocket.send('Transfer-Encoding: identity ') connectionSocket.send(LengthString.encode('utf-8')) connectionSocket.send(http_content.encode('utf-8')) print(type(outputdata)) try: outputdatae = outputdata.encode('utf-8') except AttributeError: outputdatae = outputdata connectionSocket.send(outputdatae) connectionSocket.shutdown(SHUT_RDWR) connectionSocket.close() else: print("No Request") pass if __name__ == '__main__': main()

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 Databases Questions!