监控用户“行为”的好方法是在Python中?
我正在寻找简单的 API 来获取有关用户在 Python 中使用计算机的信息。
相关的内容:
- 按键、鼠标事件 [PyKeylogger?] # 我知道这已经在 SO 中进行了辩论
- 知道有关正在运行的进程的信息
- 更困难的是,了解有关具有焦点的窗口(其名称?)的信息 # 这将是真的很有趣
- 更难的是,我可以向系统询问更多信息吗? (例如,我可以查询此窗口中特定图形元素中显示的文本吗?假设它是浏览器,我可以获得当前的 url 吗?)
我希望答案与 Linux 系统有关,但我也很感兴趣如果更容易的话,可以使用 Windows 替代方案。我现在不太关心可移植性。
我想象在 Linux 下有运行 shell 脚本并检索输出的回退,但我想知道这是否是常见的方法,或者某些 API 是否已经很好地包装了它。
对于这个问题的含糊之处,我深表歉意,但我确实是在尝试评估在某些 API 下我可以轻松获得的信息范围,并了解哪些工具对于这种类型是“常用”的工作。
感谢您提供的每一点信息。
I'm looking for easy APIs to get informations about the user use of his computer in Python.
What would be relevant:
- Keypresses, mouse events [PyKeylogger?] # I know this has been debated in SO already
- Know information about the processes that are running
- Harder, know information about the window that has focus (its name?) # that would be really interesting
- Even harder, can I ask the system for even more information? (for instance, may I query about the text displayed in a particular graphical element this window? say it's a browser, can I get the current url?)
I'd like the answers to be about a Linux system, but I am also interested in Windows alternatives if it is easier. I don't care about portability that much for now.
I imagine that under Linux there is the fallback of running a shell script and retrieving the output, but I want to know if this is the common way to do it or if some APIs wrap this nicely already.
I'm sorry for the vagueness of the question, but it's really me trying to assess the extent of information I can get easily under certain APIs and to know about what tools are "usual" for this kind of work.
Thank you for every bit of information you might bring.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题与其说是模糊的,不如说是相当广泛的。您正在寻找的大部分信息(正如您自己所指出的)分布在有关 SO 的各种问题上。
我对这个主题做了一些研究,用于我正在考虑编写的生产力跟踪应用程序。以下是我可以给您的一些一般建议:
要监视和查询窗口上的信息(标题、状态等),您需要查看libwnck (参见:python-wnck)。请注意,
WnckScreen
对象在创建时不会从 X 服务器获取信息,并且看起来屏幕上没有工作区或窗口 (更多信息请参见此处)。在某些情况下,您可能能够通过查询
/proc
中的条目来获取有关正在运行的进程的一些有用信息(您可能需要查看 psutil 模块)。但是,您更有可能不会(这取决于您要查找的内容)。要查询应用程序的非常具体的信息,您必须查看该特定应用程序支持的内容。
Linux 应用程序倾向于使用 D-Bus 服务公开相关的内部功能。
例如,Totem 有 D-Bus 服务 本质上公开了主要 Totem 对象,您可以使用它来监控正在播放的内容等。Geany,我使用的文本编辑器(或轻量级 IDE),有很多功能功能完整的 D-Bus 插件,您可以使用它来查询当前活动的选项卡等。
Firefox 在这方面有点奇怪。多年来,人们为揭露 D-Bus 服务做出了各种努力,但由于某种原因,这些努力似乎从未取得任何成果。您最好的选择是查看 MozRepl,它可以让您对 Firefox 和其他 Mozilla 进行编程来自内部的基于应用程序”(它通过 telnet 公开交互式 shell)。
至于查询个人控件/小部件的通用方法。在Linux上我只知道寄生虫。如果您使用过 Firebug,它有点像那样,但对于 GTK+ 应用程序来说。但是,我怀疑它在您的用例场景中会有多大用处。
您还可以(ab)使用辅助技术支持(用于屏幕阅读器等)。请参阅此答案。我自己对此没有任何经验。
如果您有任何疑问,请随时对此答案发表评论。
The question is not so much vague as it is rather broad. Most of the information you're looking for is (as you indicated yourself) spread out over various questions on SO.
I've done some research on this subject for a productivity tracking application I was thinking about writing. Here's some general advice I can give you:
For monitoring and querying information on windows (title, state etc.) you'll want to take a look at libwnck (see: python-wnck). Beware that at its creation a
WnckScreen
object will not have fetched information from the X server and it will look like there are no workspaces nor windows on the screen (more on this here).In some cases you might be able to get some useful information on a running process by querying its entry in
/proc
(you might want to look at the psutil module). However, it's more likely that you will not (it depends on what you're looking for).For querying very specific information on an application you'll have to look at what that specific application supports.
On Linux applications tend to expose relevant internal functionality using a D-Bus service.
Totem for example has a D-Bus service that essentially exposes the main Totem object which you can use to monitor what's playing etc.. Geany, a text editor (or lightweight IDE) I use, has a pretty much feature complete D-Bus plugin you can use to query the currently active tab etc.
Firefox is a bit of an odd duck in this respect. There have been various efforts over the years to expose a D-Bus service but for some reason those efforts never seem to have gone anywhere. Your best bet is to look at MozRepl which "lets you program Firefox and other Mozilla-based applications from the inside" (it exposes an interactive shell over telnet).
As for a generic approach to querying individuals controls/widgets. On Linux I know only of Parasite. If you've used Firebug, it's sort of like that, but for GTK+ applications. However, I have my doubts it's going to be of much use in your use case scenario.
You might also be able to (ab)use the assistive technologies support (for screen readers and such). See this answer. I don't have any experience with this myself.
If you have any questions, feel free to leave a comment on this answer.