0

So I'm working on a desktop based app that requires a socket to handle multiple authenticated clients with a Django channel, and clients should be authenticated by the server before opening the channels. The server is running on one different system, and clients are connecting to multiple systems. I have tried to connect a socket with a different platform with much effort, but I have failed to connect it. Is it possible to connect clients on different platforms to the server? If yes, please help me to resolve this issue because I am new to this technology.

My question: is using the code below, how would I be able to have multiple clients connected? I have tried lists, but I just can't figure out the format for them. How can I accomplished this where multiple clients are connected to Websocket, and l am able to send data to a specific or multiple clients? and how can I add channels in the below code?

I need your valuable suggestions or feedback would be highly appreciated.

server.py

import socket

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

serversocket.bind(('localhost', 8080))

serversocket.listen(10)  # become a server socket connections

while True:
    connection, address = serversocket.accept()
    buf = connection.recv(64)
    if len(buf) > 0:
        print(buf)
        break

Here is the client script, I don't want to send data using an API any other way to get data from the server side:

client.py
def register_user():
    username = reg_username_entry.get()
    password = reg_password_entry.get()
    if not username or not password:
        messagebox.showerror("Input Error", "Please enter a username and password.")
        return

    data = {
        'username': username,
        'password': password
    }

    try:
        response = requests.post("http://127.0.0.1:8000/api/register/", json=data)
        if response.status_code == 200:
            messagebox.showinfo("Success", response.json()['success'])
        else:
            try:
                error_message = response.json().get('error', 'Registration failed')
            except ValueError:
                error_message = response.text
            messagebox.showerror("Error", error_message)
    except requests.exceptions.RequestException as e:
        messagebox.showerror("Request failed", str(e))


def login_user():
    hostname = socket.gethostname()
    ip_address = socket.gethostbyname(hostname)

    username = login_username_entry.get()
    password = login_password_entry.get()
    if not username or not password:
        messagebox.showerror("Input Error", "Please enter a username and password.")
        return

    data = {
        'username': username,
        'password': password
    }

    try:
        response = requests.post("http://127.0.0.1:8000/api/login/", json=data)
        if response.status_code == 200:
            messagebox.showinfo("Success", response.json()['success'])
            # messagebox.showinfo("Message", "Hey There! I hope you are doing well.")
            clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            clientsocket.connect(('localhost', 8080))
            clientsocket.send(bytes('Socket Connected!', 'utf-8'))
            print(f"Hostname: {hostname}")
            print(f"IP Address: {ip_address}")

        else:
            try:
                error_message = response.json().get('error', 'Login failed')
            except ValueError:
                error_message = response.text
            messagebox.showerror("Error", error_message)
    except requests.exceptions.RequestException as e:
        messagebox.showerror("Request failed", str(e))
3
  • You said you are using Django Channels but you aren’t. You are using Python’s built-in socket library. Commented Jun 27, 2024 at 19:52
  • Hello sir , The given code is my simple client-server code. I am not using a channel, but now I want to connect the client and server using a channel on different platforms for client connectivity. How can I do it? Commented Jun 28, 2024 at 6:10
  • You say things like: "… using the code below, how would I be able to have multiple clients connected?" and things like: "Websocket". I hope you know what you are doing but I can’t answer your question because it lacks clarity. Commented Jun 28, 2024 at 6:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.