利用Python监控儿子每天在电脑上面做了些什么

在监测到玩游戏、看视频等钓鱼行为后,还会监测工人的离职倾向。

[En]

After the fishing behaviors such as playing games and watching videos have been monitored, the turnover tendency of workers will also be monitored.

有网友爆料,知乎正在低调裁员,视频相关部门也即将减半。在知乎裁员讨论区,有网友表示,企业安装了行为感知系统,可以提前知道员工的跳槽想法。

[En]

Some netizens revealed that Zhihu was laying off staff in a low profile, and video-related departments were about to cut in half. In the discussion area of Zhihu layoffs, some netizens said that enterprises have installed a behavior-aware system, which can know employees’ job-hopping thoughts in advance.

在否认裁员计划的同时,知乎还表示,它从未安装和使用过网上描述的行为感知系统,未来也不会使用类似的软件工具。

[En]

While denying the layoff plan, Zhihu also stated that it had never installed and used the behavior-aware system described online, and would not use similar software tools in the future.

正因为如此,信念被推上了风口浪尖,舆论关注度越来越高。

[En]

Because of this, conviction has been pushed to the forefront of the wind and waves, and public opinion is paying more and more attention.

一时间,”打工人太难了””毫无隐私可言”的讨论层出不穷。

今天就带大家领略一下怎么写几行 Python 代码,就能监控电脑。

干货主要有:

① 200 多本 Python 电子书(和经典的书籍)应该有

② Python标准库资料(最全中文版)

③ 项目源码(四五十个有趣且可靠的练手项目及源码)

④ Python基础入门、爬虫、网络开发、大数据分析方面的视频(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)

Python学习交流Q群101677771

监控键盘

如果公司偷偷在我们的电脑上运行了一个后台进程,来监控我们的键盘事件,最简单的 python 写法大致是这样的:

<span class="hljs-keyword">from pynput <span class="hljs-keyword">import keyboard

<span class="hljs-function"><span class="hljs-keyword">def <span class="hljs-title">on_press<span class="hljs-params">(key):
    print(<span class="hljs-string">f'<span class="hljs-subst">{key} :pushed')

<span class="hljs-function"><span class="hljs-keyword">def <span class="hljs-title">on_release<span class="hljs-params">(key):
    <span class="hljs-keyword">if key == keyboard.Key.esc:
        <span class="hljs-keyword">return <span class="hljs-literal">False

<span class="hljs-keyword">with keyboard.Listener(on_press=on_press, on_release=on_release) <span class="hljs-keyword">as lsn:
    lsn.join()

</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

随意点击键盘,您将看到控制台的输出如下:

[En]

Tap the keyboard at will, and you will see output like this from the console:

代码内容就是两个方法,一个是监听按键事件,另一个是监听退出事件——敲击 ESC 按键后释放就退出了。

监控鼠标

如果您还想监听鼠标事件,只需使用以下代码:

[En]

If you also want to listen for mouse events, just use this code:

<span class="hljs-keyword">from pynput <span class="hljs-keyword">import mouse

<span class="hljs-function"><span class="hljs-keyword">def <span class="hljs-title">on_click<span class="hljs-params">(x, y, button, pressed):
    <span class="hljs-keyword">if button == mouse.Button.left:
        print(<span class="hljs-string">'left was pressed!')
    <span class="hljs-keyword">elif button == mouse.Button.right:
        print(<span class="hljs-string">'right was pressed!')
        <span class="hljs-keyword">return <span class="hljs-literal">False
    <span class="hljs-keyword">else:
        print(<span class="hljs-string">'mid was pressed!')

</span></span></span></span></span></span></span></span></span></span></span></span></span></span>

此代码主要监控鼠标的左击和右击。运行鼠标后,您可以看到控制台打印以下结果:

[En]

This code mainly monitors the left and right click of the mouse. After running the mouse, you can see the console prints the following results:

如果仔细查看,您会发现每个单击事件都会打印两次。这是因为鼠标事件是通过按下和释放来触发的。

[En]

If you are careful, you will find that each click event is printed twice. This is because mouse events are triggered by pressing and releasing.

记录监控日志

键盘事件和鼠标事件都有了,是时候将二者结合起来,把用户的操作记录到日志了。这里我们用 loguru 来记录日志,这个 python 模块我们之前的文章也讲过。

整个代码如下:

<span class="hljs-keyword">from pynput <span class="hljs-keyword">import keyboard, mouse
<span class="hljs-keyword">from loguru <span class="hljs-keyword">import logger
<span class="hljs-keyword">from threading <span class="hljs-keyword">import Thread

</span></span></span></span></span></span>

运行后,您可以在同一目录下的日志文件中看到类似以下内容:

[En]

After running it, you can see something like this in the log file in the same directory:

本文主要通过 pynput 这个 python 模块讲解一下怎么记录键盘和鼠标的操作。这几行简单的代码对于监控输入密码之类的简单操作可以使用,但是对于聊天记录之类的复杂语句,你还需要针对日志用 NLTK 语言处理,才能复原你的聊天记录。

学了这些之后,你最想做的是什么?欢迎在评论区留言!

[En]

After learning this, what do you want to do most? Welcome to leave messages in the comment area!

Original: https://www.cnblogs.com/sn5200/p/15944954.html
Author: Python可乐的呀
Title: 利用Python监控儿子每天在电脑上面做了些什么

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/509926/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球