title: Send and Receive from and to different ports simultaneously python
body: <p><a href="https://i.stack.imgur.com/eI3xB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eI3xB.png" alt="enter image description here"></a></p>
  
  <p>I have 3 programs(<strong>clientA.py, ClientB.py, Server_X.py</strong>) in first machine and 1 program (<em>machine_Y.py</em>) in 2nd machine.<br/>
  I need to connect as a picture and send data simultaneously from two clients to the server on host Y, modify this data and send to the server in host X.
  <br/>
  <hr/>
  Cant connect to machine <strong><em>more than one socket at a time</em></strong>. says connection refused.
  <br/>
  <hr/>
  I tested my code on localhost which in this case two machines is same.
  Can you advise some solution to this problem?
  <br/></p>
  
  Client A
  
  <pre><code>import socket
  import time
  import threading
  def client_cam():
      # create socket and bind host
      client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      client_socket.connect(('localhost', 9000))
      connection = client_socket.makefile('wb')
      try:
          count=1
          while True:
              data = 'from CAMERA: '
              client_socket.send(b'Camera')
              time.sleep(1)
              count+=1
      finally:
          connection.close()
          client_socket.close()
  
  if __name__ == '__main__':
      client_cam()
  </code></pre>
  
  <p><br/>Client B is same above except port number</p>
  
  machine_Y.py
  
  <pre><code>....# here some handlers for VideoStreamHandler , SensorDataHandler
  class ThreadServer(object):
  
      def server_thread(host, port):
          server = SocketServer.TCPServer((host, port), VideoStreamHandler)
          server.serve_forever()
  
      def server_thread2(host, port):
          server = SocketServer.TCPServer((host, port), SensorDataHandler)
          server.serve_forever()
  
      def motor_client(host,port):
          client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          client_socket.connect((host, port))
          connection = client_socket.makefile('wb')
          try:
              count = 1
              while True:
                  data = 'from Motor: '
                  client_socket.send(b'motor')
                  time.sleep(1)
                  count += 1
          finally:
              connection.close()
              client_socket.close()
  
      motor_thread = threading.Thread(target=motor_client('localhost',9003))
      video_thread = threading.Thread(target=server_thread('localhost', 9000))
      distance_thread = threading.Thread(target=server_thread2('localhost', 9001))
  
      video_thread.start()
      distance_thread.start()
      motor_thread.start()
  
  if __name__ == '__main__':
      ThreadServer()
  </code></pre>
  
  Server_X.py
  
  <pre><code>import SocketServer
  import threading
  
  class MotorStreamHandler(SocketServer.BaseRequestHandler):
  
      data = " "
  
      def handle(self):
  
          try:
              while self.data:
                  self.data = self.request.recv(1024)
                  print(self.data)
          finally:
              print("Connection closed on thread Motor")
  class ThreadServer(object):
      def server_thread(host, port):
          server = SocketServer.TCPServer((host, port), MotorStreamHandler)
          server.serve_forever()
      motor_thread = threading.Thread(target=server_thread('localhost',9003))
      motor_thread.start()
  
  if __name__ == '__main__':
      ThreadServer()
  </code></pre>