Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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