title: Multiplayer Snake Game Python
body: <p>I will create a multiplayer "Snake Game" in Python. On the server I'm using threads to be able to handle multiple clients, but now I do not know what to do to send a socket to all clients. I'll need it to inform all users to the new position of "food" when someone "eat the food."</p>
  
  <p>Here is my server code:</p>
  
  <pre><code>from socket import *
  import threading, os, sys, random
  #from constants import *
  
  def isOnline(username):
      return username in playerList
  
  def getNewColor():
      r = random.randrange(0, 255)
      g = random.randrange(0, 255)
      b = random.randrange(0, 255)
      return (r, g, b)
  
  def genFood():
      x = random.randrange(1, 49)
      y = random.randrange(1, 49)
      return (x, y)
  
  def handler(clientsocket, clientaddr):
      print ('New Client ', clientaddr)
  
      player = ''
      points = 0
      index = 0
  
      while 1:
          try:
              data = clientsocket.recv(1024).decode()
              if data != '':
                  print(data)
              if data.split(' ')[0] == 'login':
                  if isOnline(data.split(' ')[1]):
                      clientsocket.send('already_on'.encode())
                  else:
                      color = getNewColor()
                      clientsocket.send(('success ' + str(color)).encode())
                      player = data.split(' ')[1]
                      index = playerList.index(player)
                      playerList.append(player)
                      pointList.append(0)
  
              if player != '':
  
                  if data == 'eat':
                      points += 1
                      pointList[index] = points
                      foodX, foodY = genFood()                                  
          except:
              print('Disconnected Client')
              clientsocket.close()
              if player != '':
                  del playerList[index]
                  del pointList[index]
              return 0
  
  addr = ('localhost', 50000)
  serversocket = socket(AF_INET, SOCK_STREAM)
  serversocket.bind(addr)
  serversocket.listen(20)
  playerList = []
  pointList = []
  
  
  while 1:
      clientsocket, clientaddr = serversocket.accept()
      threading._start_new_thread(handler, (clientsocket, clientaddr))
  
  serversocket.close()
  </code></pre>