在Windows& Windows XP中,如何通过Python获取用户登录/注销时间. Linux的?

编程入门 行业动态 更新时间:2024-10-23 19:19:17
本文介绍了在Windows& Windows XP中,如何通过Python获取用户登录/注销时间. Linux的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试开发脚本或工具来告诉每个用户占用计算机多长时间,以便管理和平衡资源.至于用户名,Python提供了一种干净的方法来通过os.getlogin()获得它.但是,我进行了搜索,但找不到一个整洁的Python API方法来获取该特定用户的登录和注销时间.对于Windows,我必须查询用户",而这不适用于Windows XP.对于Linux,我必须承认我不擅长.你们知道跨平台和Python API的方法吗?.

I am trying to develop a script or tool to tell about how long each user occupies the computer, in order to manage and balance the resource. As for the user name, Python provides clean way to get it by os.getlogin(). However, I searched and could not find a neat Python API method to get the login and logoff time of that specific user. For Windows, I have to "query user" and this does not work for windows XP. And for Linux, I have to admit I am not good at it. Do you guys know a cross-platform and Python API way to do that?.

非常感谢! 迪

推荐答案

Windows解决方案(应在xp上运行)

Windows Solution (should work on xp)

>>> import win32net,time >>> users,nusers,_ = win32net.NetUserEnum(None,2) >>> for user in users: ... print "%-20s %s"%(user['name'],time.ctime(user['last_logon'])) ... Administrator Sat Oct 06 04:59:59 2012 Bob Mon Nov 05 10:03:17 2012 Guest Wed Dec 31 16:00:00 1969 joran Fri May 10 08:04:36 2013 SophosSAUNEREUS0 Fri May 10 13:31:26 2013 Umläut Tue Nov 13 16:21:49 2012

您可以使用

import platform platform.uname()

例如,确定您所处的操作系统并为每个操作系统使用单独的方法

to determine which OS you are in and use a separate method for each OS, for example

import platform os_name = platform.uname()[0].lower() if os_name == "windows": get_win_login_time() elif os_name.endswith("nix"): get_nix_login_time() ....

回答网络用户的问题

import win32net, win32netcon, platform win32net.NetUserEnum(win32net.NetGetDCName(),2,win32netcon.FILTER_NORMAL_ACCOUNT)

更多推荐

在Windows& Windows XP中,如何通过Python获取用户登录/注销时间. Linux的?

本文发布于:2023-11-06 06:55:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户登录   时间   amp   Windows   XP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!