예제 #1
0
    def _request(self, msg):
        '''_request

        Override method _request in class ncclient.operations.RPC, so it can
        handle raw rpc requests in string format without validating your rpc
        request syntax. When your rpc-reply is received, in most cases, it
        simply returns rpc-reply again in string format, except one scenario:
        If message-id is missing or message-id received does not match that in
        rpc request, ncclient will raise an OperationError.
        '''

        logger.debug('Requesting %r' % self.__class__.__name__)
        logger.info('Sending rpc...')
        logger.info(msg)
        time1 = time.time()
        self._session.send(msg)
        if not self._async:
            logger.debug('Sync request, will wait for timeout=%r' %
                         self._timeout)
            self._event.wait(self._timeout)
            if self._event.isSet():
                time2 = time.time()
                reply_time = "{:.3f}".format(time2 - time1)
                logger.info('Receiving rpc-reply after {} sec...'.
                            format(reply_time))
                logger.info(self._reply)
                return self._reply
            else:
                logger.info('Timeout. No rpc-reply received.')
                raise TimeoutExpiredError('ncclient timed out while waiting '
                                          'for an rpc-reply.')
예제 #2
0
파일: rpc.py 프로젝트: nwautomator/ncclient
    def _request(self, op):
        """Implementations of :meth:`request` call this method to send the request and process the reply.

        In synchronous mode, blocks until the reply is received and returns :class:`RPCReply`. Depending on the :attr:`raise_mode` a `rpc-error` element in the reply may lead to an :exc:`RPCError` exception.

        In asynchronous mode, returns immediately, returning `self`. The :attr:`event` attribute will be set when the reply has been received (see :attr:`reply`) or an error occured (see :attr:`error`).

        *op* is the operation to be requested as an :class:`~xml.etree.ElementTree.Element`
        """
        self.logger.info('Requesting %r', self.__class__.__name__)
        req = self._wrap(op)
        self._session.send(req)
        if self._async:
            self.logger.debug('Async request, returning %r', self)
            return self
        else:
            self.logger.debug('Sync request, will wait for timeout=%r',
                              self._timeout)
            self._event.wait(self._timeout)
            if self._event.isSet():
                if self._error:
                    # Error that prevented reply delivery
                    raise self._error
                self._reply.parse()
                if self._reply.error is not None and not self._device_handler.is_rpc_error_exempt(
                        self._reply.error.message):
                    # <rpc-error>'s [ RPCError ]

                    if self._raise_mode == RaiseMode.ALL or (
                            self._raise_mode == RaiseMode.ERRORS
                            and self._reply.error.severity == "error"):
                        errlist = []
                        errors = self._reply.errors
                        if len(errors) > 1:
                            raise RPCError(to_ele(self._reply._raw),
                                           errs=errors)
                        else:
                            raise self._reply.error
                if self._device_handler.transform_reply():
                    return NCElement(self._reply,
                                     self._device_handler.transform_reply(),
                                     huge_tree=self._huge_tree)
                else:
                    return self._reply
            else:
                raise TimeoutExpiredError(
                    'ncclient timed out while waiting for an rpc reply.')