syspy  0.1
syspy windows system information tool with python
info.py
Go to the documentation of this file.
1 __title__ = 'syspy'
2 __version__ = '0.1'
3 __author__ = 'Ali GOREN <goren.ali@yandex.com>'
4 __repo__ = 'https://github.com/aligoren/syspy'
5 __license__ = 'Apache v2.0 License'
6 
7 import psutil
8 import datetime
9 
10 class Info:
11 
12  def __init__(self):
13  self.getUsers
14  self.getBootTime
15  self.getProcessIter
16 
17  def getUsers(self):
18  """
19  Return users currently connected on the system as a list of namedtuples including the following fields:
20 
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.
25 
26  >>> import psutil
27  >>> psutil.users()
28  [suser(name='ali', terminal=None, host='0.0.0.0', started=1430050230.0)]
29  >>> lusers = list(psutil.users())
30  >>> lusers
31  [suser(name='ali', terminal=None, host='0.0.0.0', started=1430050230.0)]
32  >>> lusers[0]
33  suser(name='ali', terminal=None, host='0.0.0.0', started=1430050230.0)
34  >>> lusers[0][1]
35  >>> lusers[0][0]
36  'ali'
37  >>> lusers[0][3]
38  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")
42  ^
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'
48  >>> import datetime
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'
53  >>>
54  """
55  lusers = psutil.users()
56 
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")
61 
62  return ret_user_name, ret_user_terminal, ret_user_host, ret_start_time
63 
64  def getBootTime(self):
65  """
66  Return the system boot time expressed in seconds since the epoch.
67  """
68  btime = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
69 
70  return btime
71 
72  def getProcessIter(self):
73  """
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.
81 
82  >>> for proc in psutil.process_iter():
83  ... try:
84  ... pinfo = proc.as_dict(attrs=['pid', 'name'])
85  ... except psutil.NoSuchProcess:
86  ... pass
87  ... else:
88  ... print(pinfo)
89  ...
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}
99  """
100  for proc in psutil.process_iter():
101  try:
102  pinfo = proc.as_dict(attrs=['pid', 'name'])
103  except psutil.NoSuchProcess:
104  pass
105  else:
106  print(pinfo)
def getProcessIter(self)
Definition: info.py:72
def getUsers(self)
Definition: info.py:17
def __init__(self)
Definition: info.py:12
def getBootTime(self)
Definition: info.py:64