so I’m working on the desktop based app that requires a socket handle multiple authenticated clients with Django channel and clients should be authenticated by server before opening the channels and server running on one different system and clients connecting multiple system. I have tried to connect socket with different platform with much effort, I have failed to connect it. is it possible to connect clients in different platform to the server? if yes please help me to resolve this issue because I am new in this technology.
my question is using the code below how would be able to have multiple clients connected? I have tried lists, but I just cant figure out the format for that. How can accomplished where multiple clients connected at Websocket and l am able to send a data to a specific or multiple clients? and how can I add channels in below 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 client script I don't want to send data using api any other way to get data from 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))
I need your valuable suggestions or feedback would be highly appreciated.
thank your !
Kaumudi Kalikar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.