def client_thread(conn): # Loop indefinitively while True: # This blocks the loop untill something is received received = conn.recv(1024) json_data = "" # Received == None if not received: print("Closing connection") break # Something was received, decode it if received: json_data = received.decode() # The decoded message is not empty, analyse it if json_data != "": # The messages use the JSON format data = json.loads(json_data) # The command is 'login', authenticate the credentials and send back the result if data["command"] == "login": tosend = {} tosend["command"] = "authlogin" tosend["auth"] = authenticate(data["username"], data["password"]) if (tosend["auth"] == "True"): auth = True print("Sending: " + json.dumps(tosend)) conn.send(json.dumps(tosend).encode()) # The command is 'getprescriptions', get the prescriptions of the user and compress them to a string # in JSON format if data["command"] == "getprescriptions" and auth: user = database.get_user_by_uid(data["uid"]) print("Info: " + str(user.id) + ", " + str(user.username) + ", " + str(user.rfid)) prescriptions = user.get_prescriptions() tosend = {} tosend["command"] = "getprescriptions" tosend["data"] = Prescription.to_json_list(prescriptions) print("Sending: " + str(tosend)) conn.send(json.dumps(tosend).encode()) # Properly close the connection, shutdown notifies the client that is should stop listening conn.shutdown(1) conn.close()