Here is an updated example of how you can modify the previous code to export the authentication logic into a separate class:
import socket
import threading
class AuthHandler:
"""Handles authentication of incoming clients"""
def __init__(self, auth_strings):
"""
auth_strings: Dict[str, Type]
Mapping of valid authentication strings to corresponding client types
"""
self.auth_strings = auth_strings
def handle(self, client_socket, addr):
"""Handles authentication of incoming clients"""
print(f"[{addr[0]}:{addr[1]}] Accepted connection")
# Prompt client for authentication string
client_socket.sendall(b"Please enter authentication string: ")
auth_string = client_socket.recv(1024).decode().strip()
if auth_string not in self.auth_strings:
print(f"[{addr[0]}:{addr[1]}] Invalid authentication string: {auth_string}")
client_socket.close()
return
# Instantiate corresponding client type in separate thread
client_type = self.auth_strings[auth_string]
client_thread = threading.Thread(target=client_type(client_socket, addr).handle)
client_thread.start()
print(f"[{addr[0]}:{addr[1]}] Authenticated as {client_type.__name__}")
class A:
"""Class for type A clients"""
def __init__(self, client_socket, addr):
self.client_socket = client_socket
self.addr = addr
def handle(self):
"""Handles incoming data from type A clients"""
while True:
data = self.client_socket.recv(1024)
if data:
# Do something with the received data
print(f"[A:{self.addr[0]}:{self.addr[1]}] Received data: {data.decode()}")
else:
# Client has disconnected
print(f"[A:{self.addr[0]}:{self.addr[1]}] Disconnected")
self.client_socket.close()
break
class B:
"""Class for type B clients"""
def __init__(self, client_socket, addr):
self.client_socket = client_socket
self.addr = addr
def handle(self):
"""Handles incoming data from type B clients"""
while True:
data = self.client_socket.recv(1024)
if data:
# Do something with the received data
print(f"[B:{self.addr[0]}:{self.addr[1]}] Received data: {data.decode()}")
else:
# Client has disconnected
print(f"[B:{self.addr[0]}:{self.addr[1]}] Disconnected")
self.client_socket.close()
break
class C:
"""Class for type C clients"""
def __init__(self, client_socket, addr):
self.client_socket = client_socket
self.addr = addr
def handle(self):
"""Handles incoming data from type C clients"""
while True:
data = self.client_socket.recv(1024)
if data:
# Do something with the received data
print(f"[C:{self.addr[0]}:{self.addr[1]}] Received data: {data.decode()}")
else:
# Client has disconnected
print(f"[C:{self.addr[0]}:{self.addr[1]}] Disconnected")
self.client_socket.close()
break
def listen(port, auth_handler):
"""Listens for incoming connections on the specified port"""
# Create a IPv4 TCP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_socket.bind(("localhost", port))
# Listen for incoming connections
server_socket.listen()
print(f"Server is listening on port {port}")
while True:
# Accept incoming connections
client_socket, client_address = server_socket.accept()
# Create a new thread to handle the client connection
client_thread = threading.Thread(target=auth_handler.handle, args=(client_socket, client_address))
client_thread.start()
if __name__ == "__main__":
# Define a mapping of valid authentication strings to corresponding client types
auth_strings = {"auth_string_A": A, "auth_string_B": B, "auth_string_C": C}
# Create an AuthHandler instance
auth_handler = AuthHandler(auth_strings)
# Define a list of ports to listen on
ports = [8000, 8001, 8002]
# Create a thread for each port to listen on
for port in ports:
t = threading.Thread(target=listen, args=(port, auth_handler))
t.start()
In this updated example, the AuthHandler class handles the authentication of incoming clients and exports the authentication logic out of the handle_client function. The class takes a dictionary of valid authentication strings and corresponding client types as input. The handle method of AuthHandler handles the authentication process and instantiates the corresponding client type in a separate thread.
The client classes (A, B, C) remain unchanged from the previous example. The listen function creates a separate thread for each port and passes the AuthHandler instance as an argument to the thread.
Note: replace "auth_string_A", "auth_string_B", and "auth_string_C" with the actual authentication strings that you want to use.