Go echo multiple ports
Jump to navigation
Jump to search
import socket import threading def handle_client(client_socket): while True: data = client_socket.recv(1024) if not data: break client_socket.send(data) client_socket.close() def start_echo_server(port): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(('localhost', port)) server.listen(5) print(f"Listening on port {port}...") while True: client_socket, _ = server.accept() client_handler = threading.Thread(target=handle_client, args=(client_socket,)) client_handler.start() if __name__ == "__main__": ports = [8080, 9090] # List of ports to listen on for port in ports: threading.Thread(target=start_echo_server, args=(port,)).start()