def Connect(self):
    """Makes the connection."""
    self._sock = socket.socket()
    self._sock.settimeout(self._connect_timeout)
    try:
      self._sock.connect((self._host, self._port))
    except socket.timeout:
      raise TimeoutError(self._connect_timeout)
    except socket.gaierror as e:
      raise ConnectionError('Lookup failure for %r: %s' % (self._host, e[1]))
    except socket.error as e:
      raise ConnectionError('Connect failure for %r: %s' % (self._host, e[1]))

    if self._initial_chat is not None:
      try:
        for expected_recv, to_send in self._initial_chat:
          actual_recv = self._sock.recv(len(expected_recv))
          if actual_recv == expected_recv:
            self._sock.send(to_send)
          else:
            raise ConnectionError('Initial chat failure for %r: expected %r, '
                                  'got %r' % (self._host, expected_recv,
                                              actual_recv))
      except socket.timeout:
        logging.debug('Initial chat timeout for %r', self._host)
        raise TimeoutError(self._connect_timeout)

    self._sock.settimeout(None)
    self.child = SocketSpawn(self._sock, maxread=8192)
    self.child.timeout = self._connect_timeout
    logging.debug('Socket connected to %r:%s', self._host, self._port)

    responses = self.child.compile_pattern_list([
        self._success,
        r'[Ll]ogin|[Uu]ser[Nn]ame',
        r'[Pp]assword:',
        r'Permission denied|Authentication failed'])
    self.exit_list = self.child.compile_pattern_list(pexpect.EOF)

    while True:
      try:
        timeout = max(1, self._connect_timeout)
        pattern = self.child.expect_list(responses, timeout=timeout)
        logging.debug('Connect() matched responses[%d]', pattern)
        if pattern == 0:
          self._MaybeFindPrompt()
          break
        elif pattern == 1:
          self.child.send(self._username+'\r')
        elif pattern == 2:
          self.child.send(self._password+'\r')
        elif pattern == 3:
          raise ConnectionError('Permission denied for %r' % self._host)
        else:
          raise ConnectionError('Unexpected pattern %d' % pattern)
      except pexpect.TIMEOUT:
        raise TimeoutError(timeout)
      except pexpect.EOF as e:
        raise ConnectionError(str(e))
    return None
dataset/ETHPy150Open google/capirca/tools/ldpush/pexpect_connection.py/SocketConnection.Connect/original