def start(self): #bind the socket print("Socket: ", self.server.fileno()) print(self.address, self.port) self.server.bind((self.address, self.port)) print("Socket Bound: ", self.server.getsockname()) print("--------------------------------------------") #listen for incoming connections self.server.listen(100) self.input = [self.server] self.output = [] while 1: #select sockets for input or output inputready,outputready,exceptready = select.select(self.input, self.output, []) for s in inputready: if s == self.server: #new connection from server was found print('Handle server socket') connectionSocket, addr = self.server.accept() print("Accepted connection from:", connectionSocket.getpeername()) #create the message queue message = Message(connectionSocket, None) self._enable_debug(message) #add to input queue and input set self.enable_input(connectionSocket, message) if(self.do_analytics): #set analyze self.analyze.open_sock(connectionSocket, addr, "client") else: try: #socket has been closed from other end if(self.in_queue[s].recv() == False): #flag done and disable input self.in_queue[s].done_receiving = True self.disable_input(s) #data is received, and ready, add it for output if(self.in_queue[s].out_ready): self.enable_output(s, self.in_queue[s]) except: #send bad request and cleanup s.send(b"400 Bad Request") self.close_sock(s) self.disable_input(s) #Handle output for s in outputready: #try: if(self.out_queue[s].outgoing != None): #ready to respond back to initiating socket if(not self.out_queue[s].send()): #nothing left to send, remove from output self.out_queue[s].end_cache() self.disable_input(self.out_queue[s].outgoing) self.disable_output(self.out_queue[s].outgoing) self.close_sock(self.out_queue[s].outgoing) self.disable_input(s) self.disable_output(s) self.close_sock(s) else: #try: #parse the data from the queue rqst, headers = self.out_queue[s].translate() send_data, host, file = self.get_command(rqst, headers) port = self._get_port(host) host = self._get_host(host) if(host != ""): #host is valid, create the socket #check for cache cache = cache_manager(host + file, self.analyze, self.do_analytics) try: if(cache.try_open_file() and (host + file) in self.cached_files): #cache file found and is flagged in cached_files try: message = Message(None, s, cache) self._enable_debug(message) message.send_from_cache() self.disable_input(s) self.disable_output(s) self.close_sock(s) except: #error occurs reading from file, get file from host self._get_web_url(s, host, file, send_data, cache) else: #no cache, get from host and add to cached_files self._get_web_url(s, host, file, send_data, cache) self.cached_files.append(host + file) except: self._get_web_url(s, host, file, send_data, None) else: #error from server, disable all print("ERROR WITH HOST:", host, send_data) #send the error s.send(send_data) self.out_queue[s].end_cache() self.disable_input(self.out_queue[s].outgoing) self.disable_output(self.out_queue[s].outgoing) if(self.out_queue[s].outgoing != None): self.close_sock(self.out_queue[s].outgoing) self.disable_input(s) self.disable_output(s) self.close_sock(s) #We are done receiving from initial socket self.disable_input(s) self.disable_output(s) '''except: print("\nException encounterd on input, closing connection\n") #send bad request and cleanup s.send(b"400 Bad Request") self.close_sock(s) self.disable_input(s) self.disable_output(s)''' if(self.do_analytics): self.analyze.display_analytics()