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(client): 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(client): data = client.recv(BUFSIZ) username = data.decode("utf-8") data = client.recv(BUFSIZ) password = data.decode("utf-8") case_login = checklogin(client) if case_login == 0: msg = "Login false" else: msg = "Login successful" client.send(bytes(msg, "utf-8")) def handle_client(client, client_addr): data = client.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 client.send(bytes(msg, "utf-8")) elif cmd == "login": login(client) case = 1 elif cmd == "quit": msg = "Quiting...\n" case = 0 client.send(bytes(msg, "utf-8")) else: msg = find_weather(cmd) case = 1 print(f"[{client_addr}] {cmd}") return case def accept_client(): server.listen() print(f"[LISTENING] Server is listening on {SERVER}") while True: client, client_addr = server.accept() thread = threading.Thread(target=handle_client, args=(client, client_addr)) thread.start() print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}") case = handle_client(client, client_addr) print(case) if case == 0: break print("[STARTING] server is starting...") accept_client() server.close()