OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to connect multiple authenticated clients' with Django channels and clients should be connected before opening the channels?

  • Thread starter Thread starter Kaumudi Kalikar
  • Start date Start date
K

Kaumudi Kalikar

Guest
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.

thank you!

Code:
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))
<p>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.</p>
<p>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?</p>
<p>I need your valuable suggestions or feedback would be highly appreciated.</p>
<p>thank you!</p>
<pre><code>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))
</code></pre>
 
Top