Python Forum

Full Version: Multi-client server
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to change this code to be multi-user.. I read a little bit about using select function instead of using threads but I can't understand how to use it in my code.

Here's my code so far:

server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   server_sock.bind((SERVER_IP, SERVER_PORT))
   server_sock.listen(1)
   client_sock, client_address = server_sock.accept()
   client_sock.sendall("Welcome to the server!")
   answer = ""

   while (True):
       msg = client_sock.recv(1024)
       if (msg == "Hello"):
           answer = "Hello"
       elif (msg == "QUIT"):
           client_sock.sendall("Bye")
           break
       else:
           answer = "Error!"
       client_sock.sendall(answer)

   client_sock.close()
   server_sock.close()
Is using raw sockets desired? Because this is exactly the sort of thing Twisted was made for.