import socket import threading BUFSIZE = 2048 PORT = 5050 SERVER = "127.0.0.1" ADDR = (SERVER, PORT) FORMAT = 'utf-8' DISCONNECT_MESSAGE = "!DISCONNECT" server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(ADDR) def checklogin(username, password): return 1 f = open('DataLogin.txt','r') num_clients = f.read() for i in range(1, num_clients + 1): login_user = f.read() login_pass = f.read() if login_user != username: case_login = 0 break else: if login_pass != password: case_login = 0 break return case_login def login(connection): data = connection.recv(BUFSIZE) username = data.decode("utf-8") data = connection.recv(BUFSIZE) password = data.decode("utf-8") case_login = checklogin(username, password) if case_login == 0: msg = "Login false" connection.send(bytes(msg, "utf-8")) return False else: msg = "Login successful" connection.send(bytes(msg, "utf-8")) return True def handle_client(connection, client_addr): data = connection.recv(BUFSIZE) cmd = data.decode("utf-8") if cmd == "help": msg = ("All commands:\n" + "Type to login the server" + "Type to register account" + "Type to leave the server") case = 1 connection.send(bytes(msg, "utf-8")) elif cmd == "login": if login(connection) == 0: case = 0 else: msg = "Welcome ....\n Enter list all/ find id/ quit" connection.send(bytes(msg, "utf-8")) case = 1 elif cmd == "quit": msg = "Quiting...\n" case = 0 connection.close() else: case = 1 print(f"[{client_addr}] {cmd}") def start(): server.listen() print(f"[LISTENING] Server is listening on {SERVER}") while True: connection, client_addr = server.accept() thread = threading.Thread(target=handle_client, args=(connection, client_addr)) thread.start() print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}") print("[STARTING] server is starting...") start() server.close()