How to do in python chat messages that are printed one client received not only the server but other clients?

here is the server code:
import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('localhost', 9090 ))
print('Server > local host')
clients = [] 

while True: 
data, addr = server_socket.recvfrom(1024)

if addr not in clients:
clients.append(addr)


print(data.decode('utf-8'))

for client in clients: 
server_socket.sendto(data, client)

server_socket.close()


here is the code client
import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.connect(('localhost', 9090))
server = ('localhost', 9090)

config = input('please enter your name: ')
client_socket.sendto(("["+config + "] => join chat ").encode("utf-8"),server)

i = False

while i == False:
message = input(':')
client_socket.sendto(('['+config + ']' + ':' + message).encode('utf-8'), server)

while True:
data = client_socket.recv(1024)




print(data.decode('utf-8'))

if not data:
break

client_socket.close()
June 3rd 19 at 19:31
1 answer
June 3rd 19 at 19:33
Solution
.recv — accepted .send — sent.
Don't forget to use .encode().
And other users can be anywhere to store.
For example, in the dictionary. So you can divide and through the rooms.
Like this when connected:

myUsers = {}
client_address, client_socket = server.accept()
myUsers.update({len(myUsers): client_socket})


This will allow You to keep the connection of all users and to work with anyone in any place, including invoking the send
So I advise not to use it .recv(1024), and just send in the beginning of the length of incoming messages in the form header.
Thus, You will be able to accept Messages of length up to 4 something billion characters.

Find more questions by tags Python