def main(): requests = [] connections = [] try: sock = socket.socket() sock.bind((host, port)) sock.settimeout(0) sock.listen(5) logging.info(f'Server started with {host}:{port}') while True: try: client, address = sock.accept() logging.info(f'Client detected {address}') connections.append(client) except Exception: pass if connections != []: rlist, wlist, xlist = select.select(connections, connections, connections, 0) for r_client in rlist: thread = threading.Thread(target=read_client_data, args=(r_client, requests, settings.BUFFERSIZE)) thread.start() if requests: b_request = requests.pop() print(b_request) if b_request != b'': b_response = handle_request(b_request) for w_client in wlist: thread = threading.Thread(target=write_client_data, args=(w_client, b_response)) thread.start() except KeyboardInterrupt: logging.info('Server closed')
def main(): requests = [] connections = [] try: sock = socket.socket() sock.bind((host, port)) sock.settimeout(0) sock.listen(5) logging.info(f'Server started with {host}:{port}') while True: try: client, address = sock.accept() logging.info(f'Client detected {address}') connections.append(client) except Exception: pass if connections != []: rlist, wlist, xlist = select.select(connections, connections, connections, 0) for r_client in rlist: b_request = r_client.recv(settings.BUFFERSIZE) requests.append(b_request) if requests: b_request = requests.pop() b_response = handle_request(b_request) for w_client in wlist: w_client.send(b_response) except KeyboardInterrupt: logging.info('Server closed')
def do_GET(self): response = handle_request(self, self.routes) response.handle(self)