code: def _read_in_thread(address, pty, blocking):
      
      client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      client.connect(address)
  
      while 1:
          data = pty.read(4096, blocking=blocking)
  
          if not data and not pty.isalive():
              while not data and not pty.iseof():
                  data += pty.read(4096, blocking=blocking)
  
              if not data:
                  try:
                      client.send(b'')
                  except socket.error:
                      pass
                  break
          try:
              client.send(data)
          except socket.error:
              break
  
      client.close()
docs: Read data from the pty in a thread.