kivy app freezes when trying to connect it with socket server

17 views Asked by At

I have a kivy app and when I try to use socket.send() and socket.recive() it just becomes unresponsive and the screen turns black. I am a complete begginer to kivy but I dont see any reason for it to just loke crush when trying to send and recive data from the server. I dont want to change the server coding to something different than socket so I just want to know how to stop it from happening and make my kivy app work with a server connection the code works for everything before the send function in the app code. but when I get to the send it dosent send anything, the app freezes anf the server dosent print anything

I've tried to use threads and run the function but it didnt work. it did help the app to not become unresponsive but the app dosent send anything and the whole app logic crumbles because the second half of the function dosent work and basiclly its like its not even written there my python code:

class login_screen(MDScreen):
global is_login
is_login = None
def show_password(self, checkbox, value):
    if value:
        self.ids.text_field_pass.password = False
        self.ids.show_pass.text = "hide pass"
    else:
        self.ids.text_field_pass.password = True
        self.ids.show_pass.text = "show pass"
#checks login info
def send_login(self):
    global is_login
    is_login = None
    username = self.ids.text_field_username.text
    password = self.ids.text_field_pass.text
    password_hash = hashlib.sha256(password.encode()).hexdigest()
    if username == "username":
        is_login = False
        self.ids.error_message.text = "please enter a username"
        return 0
    if password == "password":
        is_login = False
        self.ids.error_message.text = "please enter a password"
        return 0
    try:
        my_socket.send("logn".encode())
        my_socket.send(send_fill(str(username)).encode())
        my_socket.send(send_fill(password_hash).encode())

        # Wait for response from the server
        answer = my_socket.recv(1)
        print("Received answer from server:", answer)

        if answer == '1':  # Note the use of bytes literal here
            is_login = True
            return 1
        elif answer == '0':
            is_login = False
            self.ids.error_message.text = recive()
            return 0
        else:
            # Handle unexpected response from server
            is_login = False
            self.ids.error_message.text = "Unexpected response from server"
            return 0
    except Exception as e:
        # Handle any exceptions that occur during communication with the server
        print("Error:", e)
        is_login = False
        self.ids.error_message.text = "Error communicating with server"
        return 0

server code is (for now):

import psycopg2
import socket
#server connection
server_socket = socket.socket()

def send_fill(text):
    text_len = len(text)
    text_len_fill = str(text_len).zfill(10)
    full_text = text_len_fill + text
    return full_text

def recive():
    reply_len = Client_socket.recv(7).decode()
    text = Client_socket.recv(reply_len).decode()
    return text

server_socket.bind(("0.0.0.0", 8820))
server_socket.listen()
print("server is up and running")
(Client_socket, Client_adress) = server_socket.accept()
print("client is connected")
print("waiting for client...")
#connection to sql here, removed it from the code to add here
###################################################


def check_login():
        cur.execute("""SELECT * FROM users""")
        data = cur.fetchall()
        username = recive()
        print("recived " + username)
        password_hash = recive()
        print("recived " + password_hash)
        for i in data:
            if username in i and password_hash in i:
                is_login = 1
                Client_socket.send(str(is_login).encode())
                return 0
                break
        is_login = 0
        Client_socket.send(str(is_login) + send_fill("wrong username or password"))


while True:
    if Client_socket.recv(4) == "logn":
        check_login()


Client_socket.close()
server_socket.close()```

0

There are 0 answers