def __init__(self, ip_address, log_handle):
        self._ipaddr = ip_address
        self.log = log_handle

        # Open socket connection to Signaling Tester
        self.log.info("Opening Socket Connection with "
                      "Signal Generator MG3710A ({}) ".format(self._ipaddr))
        try:
            self._sock = socket.create_connection((self._ipaddr, 49158),
                                                  timeout=30)
            self.send_query("*IDN?", 60)
            self.log.info("Communication Signal Generator MG3710A OK.")
            self.log.info("Opened Socket connection to ({})"
                          "with handle ({})".format(self._ipaddr, self._sock))
        except socket.timeout:
            raise AnritsuError("Timeout happened while conencting to"
                               " Anritsu MG3710A")
        except socket.error:
            raise AnritsuError("Socket creation error")
    def send_query(self, query, sock_timeout=10):
        """ Sends a Query message to Anritsu MG3710A and return response

        Args:
            query - Query string

        Returns:
            query response
        """
        self.log.info("--> {}".format(query))
        querytoSend = (query + TERMINATOR).encode('utf-8')
        self._sock.settimeout(sock_timeout)
        try:
            self._sock.send(querytoSend)
            result = self._sock.recv(256).rstrip(TERMINATOR.encode('utf-8'))
            response = result.decode('utf-8')
            self.log.info('<-- {}'.format(response))
            return response
        except socket.timeout:
            raise AnritsuError("Timeout: Response from Anritsu")
        except socket.error:
            raise AnritsuError("Socket Error")
    def send_command(self, command, sock_timeout=30, opc=True):
        """ Sends a Command message to Anritsu MG3710A

        Args:
            command - command string

        Returns:
            None
        """
        self.log.info("--> {}".format(command))
        cmdToSend = (command + TERMINATOR).encode('utf-8')
        self._sock.settimeout(sock_timeout)
        try:
            self._sock.send(cmdToSend)
            if opc:
                # check operation status
                status = self.send_query("*OPC?")
                if int(status) != OPERATION_COMPLETE:
                    raise AnritsuError("Operation not completed")
        except socket.timeout:
            raise AnritsuError("Timeout for Command Response from Anritsu")
        except socket.error:
            raise AnritsuError("Socket Error for Anritsu command")
        return