在Windows& Windows XP中,如何通过Python获取用户登录/注销时间. Linux的? [英] How to get the user login/logoff time by Python for both Windows & Linux?

查看:188
本文介绍了在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的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆