3 __author__ =
'Ali GOREN <goren.ali@yandex.com>'
4 __repo__ =
'https://github.com/aligoren/syspy'
5 __license__ =
'Apache v2.0 License'
19 Return users currently connected on the system as a list of namedtuples including the following fields:
21 user: the name of the user.
22 terminal: the tty or pseudo-tty associated with the user, if any, else None.
23 host: the host name associated with the entry, if any.
24 started: the creation time as a floating point number expressed in seconds since the epoch.
28 [suser(name='ali', terminal=None, host='0.0.0.0', started=1430050230.0)]
29 >>> lusers = list(psutil.users())
31 [suser(name='ali', terminal=None, host='0.0.0.0', started=1430050230.0)]
33 suser(name='ali', terminal=None, host='0.0.0.0', started=1430050230.0)
39 >>> lusers[0][3]..strftime("%Y-%m-%d %H:%M:%S")
40 File "<stdin>", line 1
41 lusers[0][3]..strftime("%Y-%m-%d %H:%M:%S")
43 SyntaxError: invalid syntax
44 >>> lusers[0][3].strftime("%Y-%m-%d %H:%M:%S")
45 Traceback (most recent call last):
46 File "<stdin>", line 1, in <module>
47 AttributeError: 'float' object has no attribute 'strftime'
49 >>> datetime.datetime.fromtimestamp(lusers[0][3].strftime("%Y-%m-%d %H:%M:%S"))
50 Traceback (most recent call last):
51 File "<stdin>", line 1, in <module>
52 AttributeError: 'float' object has no attribute 'strftime'
55 lusers = psutil.users()
57 ret_user_name = lusers[0][0]
58 ret_user_terminal = lusers[0][1]
59 ret_user_host = lusers[0][2]
60 ret_start_time = datetime.datetime.fromtimestamp(lusers[0][3]).strftime(
"%Y-%m-%d %H:%M:%S")
62 return ret_user_name, ret_user_terminal, ret_user_host, ret_start_time
66 Return the system boot time expressed in seconds since the epoch.
68 btime = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime(
"%Y-%m-%d %H:%M:%S")
74 Return an iterator yielding a Process class instance for all running processes on the local machine.
75 Every instance is only created once and then cached into an internal
76 table which is updated every time an element is yielded.
77 Cached Process instances are checked for identity so that you’re
78 safe in case a PID has been reused by another process,
79 in which case the cached instance is updated.
80 This is should be preferred over psutil.pids() for iterating over processes.
82 >>> for proc in psutil.process_iter():
84 ... pinfo = proc.as_dict(attrs=['pid', 'name'])
85 ... except psutil.NoSuchProcess:
90 {'pid': 0, 'name': 'System Idle Process'}
91 {'pid': 4, 'name': 'System'}
92 {'pid': 216, 'name': None}
93 {'pid': 252, 'name': None}
94 {'pid': 404, 'name': None}
95 {'pid': 488, 'name': None}
96 {'pid': 500, 'name': None}
97 {'pid': 544, 'name': None}
98 {'pid': 568, 'name': None}
100 for proc
in psutil.process_iter():
102 pinfo = proc.as_dict(attrs=[
'pid',
'name'])
103 except psutil.NoSuchProcess: