zookeeper_collector.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:ops_agent 作者: sjqzhang 项目源码 文件源码
def _send_command(self, command, host, port, timeout):
        sock = socket.socket()
        sock.settimeout(timeout)
        buf = StringIO.StringIO()
        chunk_size = 4096
        # try-finally and try-except to stay compatible with python 2.4
        try:
            try:
                # Connect to the zk client port and send the stat command
                sock.connect((host, port))
                sock.sendall(command)

                # Read the response into a StringIO buffer
                chunk = sock.recv(chunk_size)
                buf.write(chunk)
                num_reads = 1
                max_reads = 100
                while chunk:
                    if num_reads > max_reads:
                        # Safeguard against an infinite loop
                        raise Exception("Read %s bytes before exceeding max reads of %s. "
                                        % (buf.tell(), max_reads))
                    chunk = sock.recv(chunk_size)
                    buf.write(chunk)
                    num_reads += 1
            except (socket.timeout, socket.error):
                raise Exception("the execution of command is timeout")
        finally:
            sock.close()
        return buf
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号