Pythony应用(01)-学习监控(04)

UC02–不定期的记录孩子的电脑桌面

US06-按时间随机抓屏:

  1. 抓屏对象Capture:

(1)从【0,59】中随机采样sampling_times_minute个数字
(2)从小到大排序
(3)如果当前时间的秒大于最大的随机数,则每次休眠0.4秒,知道小于等于为止
(4)遍历随机数字,如果当前秒小于随机数,则等待0.4秒,直到相等。
(5)抓屏幕,保存为图片文件

  • sampling_times_minute 每分钟采样次数
  • capture() 实时不停的抓拍
  • random_nums() 从【0,59】中随机采样sampling_times_minute个数字

class Capture:
    sampling_times_minute = 1  # 每分钟采样次数

    @classmethod
    def capture(cls):
        while True:
            secs = sorted(cls.random_nums())
            cur_time = time.localtime()
            cls.logger.info("采集日志的时刻(秒):%s", str(secs))
            while secs[-1] < cur_time.tm_sec:
                time.sleep(0.4)
                cur_time = time.localtime()

            for sec in secs:
                cur_time = time.localtime()
                if sec < cur_time.tm_sec:
                    continue
                while sec > cur_time.tm_sec:
                    time.sleep(0.4)
                    cur_time = time.localtime()

                filename = cls.get_filename()
                im = ImageGrab.grab()
                im.save(filename)
                cls.logger.info("生成文件:%s", filename)

    @classmethod
    def random_nums(cls):
        return random.sample(range(0, 60), cls.sampling_times_minute)

US07-查看最新抓屏:

  1. Web服务对象:MonitorPageWebService
  • new_capture_pic() 返回最新的图片
  • main_run.new_capture_pic() URL映射

class MonitorPageWebService:
    ......

    @classmethod
    def new_capture_pic(cls):
        filenames = os.listdir(cls.save_capture_pic_path)
        filenames.sort(reverse=True)
        new_filename = os.path.join(cls.save_capture_pic_path, filenames[0])
        return send_file(new_filename, mimetype='image/png')

    ......
    @classmethod
    def main_run(cls):
    ......

        @app.route('/new_capture_pic')
        def new_capture_pic():
            return cls.new_capture_pic()

    ......

US08-图片保存:

  1. 抓屏对象:Capture
  • save_pic_path图片保存位置
  • get_filename():在html代码中添加img的点击事件,获取新的图片,通过参数做到不从缓冲中获取图片

class Capture:
    save_pic_path = os.path.join(os.path.dirname(__file__),
                                 "static", "capture_pic")

    logger = MyLogger.get("Capture", level=logging.DEBUG)

    @classmethod
    def capture(cls):
        while True:
                ......
                filename = cls.get_filename()
                im = ImageGrab.grab()
                im.save(filename)
                cls.logger.info("生成文件:%s", filename)

    @classmethod
    def get_filename(cls):
        now_str = time.strftime("%Y%m%d_%H%M%S")
        filename = os.path.join(cls.save_pic_path, f"capture_{now_str}.png")
        return filename

US09-点击刷新图片:

  1. Web服务对象:MonitorPageWebService
  • get_html():在html代码中添加img的点击事件,获取新的图片,通过参数做到不从缓冲中获取图片


class MonitorPageWebService:
    ......

    @classmethod
    def get_html(cls, form_field):
        html = """
    ......
                    <td>
                        <img id="displayPic" src="{{ cur_handler.imgSrc }}" 
                        width="100%" height="100%" alt="monitor capture" 
                        οnclick="img_display_pic_onclick(this)">
                    </td>
    ......
                <script type="text/javascript">
                    function img_display_pic_onclick(elem){
                        var src = elem.src.split('?')[0];
                        src = src + '?' + (new Date().getTime());
                        elem.src = src;
                    }
                </script>
            </body>
            </html>
        """

    ......
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mengyoufengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值