title: Send TCP message at random time to one of several clients in python
body: <p>I am trying to send a TCP message at random time, depending on when the user input is received, to one of several clients from which I am receiving data on a thread for each client.</p>
  
  <p>I am storing the IP of each client inside an SQL database and I decide to which IP i want to send data based on user input. How should I approach sending a message to a specific client ? I don't know how I can gain access over each socket connection and use it to send messages.</p>
  
  <p>Any device, links of examples, or code snippets are greatly welcome.</p>
  
  <p>This is the server code that I have so far :</p>
  
  <pre><code>import socket
  import threading
  from database import Database
  
  d = Database()
  def Read_RFID_tag():
      while True:
          receiveID = raw_input("tag input : ")
          d.updateTable(receiveID, 1);
          print(receiveID)
  
  class ThreadedServer(object):
      def __init__(self, host, port):
          self.host = host
          self.port = port
          self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
          self.sock.bind((self.host, self.port))
  
      def listen(self):
          self.sock.listen(5)
          while True:
              client, address = self.sock.accept()
              print("new client")
              client.settimeout(60)
              threading.Thread(target = self.listenToClient,args = (client,address)).start()
  
      def listenToClient(self, client, address):
          size = 1024
          while True:
              query_result = d.existsWashedInTable(address)
              try:
                  data = client.recv(size)
                  if data:
                      # Set the response to echo back the received data
                      print(data)
                      response = data
                      d.addEntryToTable(response,address, 0)
                      client.send(response)
                  else:
                      raise error('Client disconnected')
              except:
                  client.close()
                  return False
  
  if __name__ == "__main__":
      port_num = 5005
      d.createTable()
      threading.Thread(target=Read_RFID_tag).start()
      ThreadedServer('',port_num).listen()
  </code></pre>