MCPcopy Create free account
hub / github.com/ActiveState/code / uptime

Function uptime

recipes/Python/496815_Getting_uptime_windows/recipe-496815.py:1–25  ·  view source on GitHub ↗

Returns a datetime.timedelta instance representing the uptime in a Windows 2000/NT/XP machine

()

Source from the content-addressed store, hash-verified

1def 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
27if __name__ == '__main__':
28 print uptime()

Callers 1

recipe-496815.pyFile · 0.85

Calls 7

startswithMethod · 0.80
datetimeMethod · 0.80
readlinesMethod · 0.45
closeMethod · 0.45
stripMethod · 0.45
splitMethod · 0.45
lowerMethod · 0.45

Tested by

no test coverage detected