コード例 #1
0
    def run(self):
        self._log.info('Watch %s for new data.' % self.extension)

        while True:
            try:
                self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                if self.authmethod == "basic":
                    self.client = ssl.wrap_socket(self.sock)
                elif self.authmethod == "cert":
                    self.client = ssl.wrap_socket(
                        self.sock,
                        keyfile=self.config['client_cert_key_path'],
                        certfile=self.config['client_cert_path'])
                else:
                    raise KeyError('No authentication mechanisms defined')
                self._log.debug('Connecting to %s %i' % (self.host, self.port))
                # self.client.settimeout(10)
                self.client.connect((self.host, self.port))

            except socket.error, exc:
                self._log.exception('unable to connect to %s: %s' %
                                    (self.host, exc))
                raise

            except KeyError:
                raise KeyError('No authentication mechanisms defined')
コード例 #2
0
    def run(self):
        self._log.info('Watch %s for new data.' % self.extension)

        while True:
            try:
                self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.client = ssl.wrap_socket(
                    self.sock,
                    ssl_version=ssl.PROTOCOL_TLSv1_2,  # pylint: disable=no-member
                    ciphers="DES-CBC3-SHA")
                self._log.debug('Connecting to %s %i' % (self.host, self.port))
                # self.client.settimeout(10)
                self.client.connect((self.host, self.port))

            except socket.error, exc:
                self._log.exception('unable to connect to %s: %s' %
                                    (self.host, exc))
                raise

            self.client.send("GET %s HTTP/1.1\r\nHost: %s\r\n%s\r\n\r\n" %
                             (self.extension, self.host, self.authhead))

            readers = [self.client]
            writers = out_of_band = []

            pending = b''

            parser = HttpParser()
            self._log.debug("+")

            while not parser.is_headers_complete():
                self._log.debug(".")
                try:
                    chunk = self.client.recv(io.DEFAULT_BUFFER_SIZE)
                except socket.error, exc:
                    err = exc.args[0]
                    self._log.debug('a recv err (%s): %s' % (err, exc))
                    break
                if not chunk:
                    self._log.exception('a No response from %s' %
                                        self.extension)
                    break
                self._log.debug('a chunk %s' % chunk)
                nreceived = len(chunk)
                nparsed = parser.execute(chunk, nreceived)
                if nparsed != nreceived:
                    self._log.exception('a nparsed %i != nreceived %i' %
                                        (nparsed, nreceived))
                    break
コード例 #3
0
    def run(self):
        self._log.info('Watch %s for new data.' % self.extension)

        while True:
            try:
                self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                if self.authmethod == "basic":
                    self.client = ssl.wrap_socket(self.sock)
                elif self.authmethod == "cert":
                    self.client = ssl.wrap_socket(
                        self.sock,
                        keyfile=self.config['client_cert_key_path'],
                        certfile=self.config['client_cert_path'])
                else:
                    raise KeyError('No authentication mechanisms defined')
                self._log.debug('Connecting to %s %i' % (self.host, self.port))
                # self.client.settimeout(10)
                self.client.connect((self.host, self.port))

            except socket.error as exc:
                self._log.exception('unable to connect to %s: %s' %
                                    (self.host, exc))
                raise

            except KeyError:
                raise KeyError('No authentication mechanisms defined')

            if self.authhead is not None:
                self.client.send("GET %s HTTP/1.1\r\nHost: %s\r\n%s\r\n\r\n" %
                                 (self.extension, self.host, self.authhead))
            else:
                self.client.send("GET %s HTTP/1.1\r\nHost: %s\r\n\r\n" %
                                 (self.extension, self.host))

            readers = [self.client]
            writers = out_of_band = []

            pending = b''

            parser = HttpParser()
            self._log.debug("+")

            while not parser.is_headers_complete():
                self._log.debug(".")
                try:
                    chunk = self.client.recv(io.DEFAULT_BUFFER_SIZE)
                except socket.error as exc:
                    err = exc.args[0]
                    self._log.debug('a recv err (%s): %s' % (err, exc))
                    break
                if not chunk:
                    self._log.exception('a No response from %s' %
                                        self.extension)
                    break
                self._log.debug('a chunk %s' % chunk)
                nreceived = len(chunk)
                nparsed = parser.execute(chunk, nreceived)
                if nparsed != nreceived:
                    self._log.exception('a nparsed %i != nreceived %i' %
                                        (nparsed, nreceived))
                    break
            self._log.debug('parser headers complete %s' %
                            parser.get_headers())
            while True:
                self._log.debug("-")
                try:
                    readable, _, _ = select.select(readers, writers,
                                                   out_of_band)
                except select.error as exc:
                    self._log.debug("b select error: %s" % exc)
                if not readable:
                    self._log.debug('b not readable')
                    break
                try:
                    chunk = self.client.recv(io.DEFAULT_BUFFER_SIZE)
                except socket.error as exc:
                    err = exc.args[0]
                    self._log.debug('b recv err (%s): %s' % (err, exc))
                    break
                if not chunk:
                    self._log.debug('b not chunk')
                    self.client.close()  # pylint: disable=no-member
                    break
                nreceived = len(chunk)
                self._log.debug('b chunk %s' % chunk)
                self._log.debug("repr: %s" % repr(chunk))
                if re.match(r'0\r\n\r\n', chunk, re.M):
                    self._log.debug('b end end end')
                    break
                nparsed = parser.execute(chunk, nreceived)
                if nparsed != nreceived:
                    self._log.exception('b nparsed %i != nreceived %i' %
                                        (nparsed, nreceived))
                    break
                data = pending + parser.recv_body()
                msg = "DATA: %s" % data
                self._log.debug(msg)
                lines = data.split(b'\n')
                pending = lines.pop(-1)
                for line in lines:
                    trigger_payload = self._get_trigger_payload_from_line(line)
                    if trigger_payload == 0:
                        pass
                    else:
                        self._log.info('Triggering Dispatch Now')
                        self._sensor_service.dispatch(trigger=self.TRIGGER_REF,
                                                      payload=trigger_payload)
            self._log.debug('main loop done')
            self.client.close()  # pylint: disable=no-member