Returns a datetime.timedelta instance representing the uptime in a Windows 2000/NT/XP machine
()
| 1 | def uptime(): |
| 2 | """Returns a datetime.timedelta instance representing the uptime in a Windows 2000/NT/XP machine""" |
| 3 | import os, sys |
| 4 | import subprocess |
| 5 | if not sys.platform.startswith('win'): |
| 6 | raise RuntimeError, "This function is to be used in windows only" |
| 7 | cmd = "net statistics server" |
| 8 | p = subprocess.Popen(cmd, shell=True, |
| 9 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 10 | (child_stdin, child_stdout) = (p.stdin, p.stdout) |
| 11 | lines = child_stdout.readlines() |
| 12 | child_stdin.close() |
| 13 | child_stdout.close() |
| 14 | lines = [line.strip() for line in lines if line.strip()] |
| 15 | date, time, ampm = lines[1].split()[2:5] |
| 16 | #print date, time, ampm |
| 17 | m, d, y = [int(v) for v in date.split('/')] |
| 18 | H, M = [int(v) for v in time.split(':')] |
| 19 | if ampm.lower() == 'pm': |
| 20 | H += 12 |
| 21 | import datetime |
| 22 | now = datetime.datetime.now() |
| 23 | then = datetime.datetime(y, m, d, H, M) |
| 24 | diff = now - then |
| 25 | return diff |
| 26 | |
| 27 | if __name__ == '__main__': |
| 28 | print uptime() |
no test coverage detected