title: Python : 2 sockets, sending from A to B and from B to A
body: <p>I've been working on this for 2 days and still I do not manage to get it to work properly. </p>
  
  <p>I wanted to write an application that uses 2 sockets with a medium in the middle</p>
  
  <p>This medium is this script that should read from socketA and write to SocketB and read from SocketB and write to SocketA. </p>
  
  <p>However it seems I can not nail it. </p>
  
  <p>My script when running accepts connections, but it will not allow me to input something on the telnet screen. </p>
  
  <p>I am using 2 shared lists between the sockets to pass data.</p>
  
  <pre><code>    #!/usr/bin/env python
      import sys
      import arduinoReadThread
      import arduinoWriteThread
      import socket
      import thread
  
  
  
      bolt = 0
      socketArray=list()
      HOST =""
      HOST2=""
      PORT1 =50115
      PORT2 =50125
  
  
  
      s1=socket.socket(socket.AF_INET, socket.SOCK_STREAM ) #create an INET, STREAMing socket
      s1.bind((HOST,PORT1)) #bind to that port
      s1.listen(2) #listen for user input and accept 1 connection at a time.
      socketArray.append(s1)
      s2=socket.socket(socket.AF_INET, socket.SOCK_STREAM ) #create an INET, STREAMing socket
      s2.bind((HOST2,PORT2)) #bind to that port
      s2.listen(2) #listen for user input and accept 1 connection at a time.
      socketArray.append(s2)
      print "sockets set up"
  
      s1ToWriteList = list()
  
  
      s2ToWriteList = list()
  
  
      def socketFunctionWrite1():
              while(bolt == 0):
                  client, address = s1.accept()
  
                  print "Writing connections"
                  if len(s1ToWriteList) &gt; 0:
                      client.send(s1ToWriteList.pop(0))
  
  
      def socketFunctionRead1():
              while(bolt == 0):
                  client2, address = s2.accept()
  
                  f = client2.recv(1024)
  
                  print "reading connection"
                  s1ToWriteList.append(f)
                  print len(s1ToWriteList)
  
      def socketFunctionWrite2():
              while(bolt == 0):
                  client2, address = s2.accept()
                  print "Writing connections"
                  if len(s2ToWriteList) &gt; 0:
                      client2.send(s2ToWriteList.pop(0))
  
  
  
      def socketFunctionRead2():
              while(bolt == 0):
                  client, address = s1.accept()
                  f = client.recv(1024)
              print "reading connection"
              s2ToWriteList.append(f)
              print len(s2ToWriteList)
  
  
  
  
  def shutDown():
          test = raw_input("Quit ?")
          if(test =="y"):
              bolt = 1
          else:
              shutDown()
  
  def spreadSockets():
  
  
          thread.start_new_thread(socketFunctionRead1,())
          print "launch 1"
          thread.start_new_thread(socketFunctionRead2,())
          print "launch 2"
          thread.start_new_thread(socketFunctionWrite1,())
          print "launch 3"
          thread.start_new_thread(socketFunctionWrite2,())
  
          print "launch 4"
  
  
  
  
  spreadSockets()
  while(True):
          pass
  </code></pre>