title: Python TCP Server Accepting Connections and Broadcasting Commands
body: <p>I've been working on a game using a number of Raspberry Pis, Python, and some buttons/switches.  My game requires a central server that issues commands to multiple clients.</p>
  
  <p>I'm not new to programming but new to Python and lower-er level network communication and have been lost in the weeds for the past 2 days on how exactly to write my server code.</p>
  
  <p>The client program is a simple socket.connect and then waits for data to be sent. No problems there.</p>
  
  <p>I have had a tough time determining exactly how to write and how to make work the server.</p>
  
  <p>Here's what my server code looks like at the moment:</p>
  
  <pre class="lang-python prettyprint-override"><code>import socket, time, sys
  import threading
  
  TCP_IP = ''
  TCP_PORT = 8888
  BUFFER_SIZE = 1024
  CLIENTS = {}
  clientCount = 0
  
  def listener():
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind((TCP_IP,TCP_PORT))
      s.listen(5)
      while True:
          conn, addr = s.accept()
          print("new connection from:"+ str(addr))
          #print(len(CLIENTS))
          global clientCount
          clientCount = clientCount+1
          print (clientCount)
          # register client
          CLIENTS[conn.fileno()] = conn
  
  
  def broadcast():
       for client in CLIENTS.values():
              client.send('this is a broadcats msg')
  
  if __name__ == '__main__':
      listener()
  
      while clientCount &gt; 0:
          broadcast()
          print(len(CLIENTS)) #print out the number of connected clients every 5s
          time.sleep(5) 
  </code></pre>
  
  <p>Here's the desired flow:
  1. Server starts and waits for first connection or more. I believe this "Server" should be running on a background thread?
  2. If <code>connectionCount &gt; 0</code> start the main program loop
  3. For now the main program loop should just display the number of connected clients and broadcast a message to all of them every 5 seconds.</p>
  
  <p>I have about 5 versions of this server.  I have tried async, select.select, and several threaded approaches but can't quite nail down the behavior I seek.  Should I be putting the server in a background thread?  If so how to broadcast to all the connections?</p>
  
  <p>The only thing I haven't tried is Twisted and that is because I could not get it installed in Windows... so I'm ruling that optino out for the moment. If anyone has a pointer on where to go on this I would really appreciate it!</p>
  
  <p><strong>Update</strong></p>
  
  <p>Ok, based on @Armans suggestion I have updated my code so that there is a server class but it still performs the same.</p>
  
  <pre><code>class server():
  
      def __init__(self):
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          s.bind((TCP_IP,TCP_PORT))
          s.listen(10)
          while 1:
              client_socket, addr = s.accept()
              print ('Connected with ' + addr[0] + ':' + str(addr[1]))
              global clientCount
              clientCount = clientCount+1
              print (clientCount)
              # register client
              CLIENTS[client_socket.fileno()] = client_socket
              threading.Thread(target=self.handler, args=(client_socket, addr)).start() 
  
  
  
      def handler(self, client_socket, addr):
          while 1:
              data = client_socket.recv(BUFFER_SIZE)
              print ('Data : ' + repr(data) + "\n")
              data = data.decode("UTF-8")
  
  
      def broadcast(self, message):
          for c in self.CLIENTS:
              c.send(message.encode("utf-8"))
  
  if __name__ == '__main__':
      s = server() #create new server listening for connections
  
      while clientCount &gt; 0:
          s.broadcast('msg here')
          print(len(CLIENTS)) #print out the number of connected clients every 5s
          time.sleep(5) 
  </code></pre>
  
  <p>I can connect multiple clients and the console displays the following:</p>
  
  <pre><code>Connected with 10.0.0.194:38406
  1
  Connected with 10.0.0.169:36460
  2
  </code></pre>
  
  <p>But the code in the "while clientCount" loop never runs. This is the zone where I have been stuck for some time so if you have a couple more ideas I would love to here any ideas!</p>