From socket Import * from multiprocessing import Process import re def fun (client_socket): DocumentRoot = './html ' Client_data = CLIENT_SOCKET.RECV (1024) Client_data_lines = Client_data.splitlines () for line in Client_data_lin Es:print (line) Httprequestmethodline = Client_data_lines[0].decode (' utf-8 ') GetFileName = Re.match ("[^/]
+ (/[^]*) ", Httprequestmethodline). Group (1) print ('-------------filename =%s '%getfilename) if GetFileName = = '/': GetFileName = documentroot + '/index.html ' else:getfilename = documentroot + getfilename print ( ' filename is%s '-----'%getfilename ' try:f = open (getfilename) except Ioerror:responseheaderlines = ' http/1.1 404 Not found\r\n ' responseheaderlines + = ' \ r \ n ' responsebody = ' Sorry file not found ' El Se:responseheaderlines = ' http/1.1 ok\r\n ' responseheaderlines + = ' \ r \ n ' responsebody = F.rea
D () F.close () Finally:response = Responseheaderlines + responsebody client_socket.send (response.encode (' Utf-8 ')) Client_socket.close () if __name__ = = ' __main__ ': server_socket = socket (af_inet,sock_stream) Server_socket.s Etsockopt (sol_socket,so_reuseaddr,1) Addr = (', 8009) server_socket.bind (ADDR) Server_socket.listen () Whil
E true:client,c_addr = server_socket.accept () p = Process (target=fun,args= (client,)) P.start ()
Client.close ()