我正在尝试在我的 irc 机器人中显示系统正常运行时间。我正在使用的脚本是:
#linux
import os, sys
from datetime import timedelta
from util import hook
import subprocess
import datetime
@hook.command
def uptime_linux(inp,say=None):
with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.readline().split()[0])
uptime_string = str(timedelta(seconds = uptime_seconds))
say(uptime_string)
# windows
def uptime():
"""Returns a datetime.timedelta instance representing the uptime in a Windows 2000/NT/XP machine"""
if not sys.platform.startswith('win'):
raise RuntimeError, "This function is to be used in windows only"
cmd = "net statistics server"
p = subprocess.Popen(cmd, shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(child_stdin, child_stdout) = (p.stdin, p.stdout)
lines = child_stdout.readlines()
child_stdin.close()
child_stdout.close()
lines = [line.strip() for line in lines if line.strip()]
date, time, ampm = lines[1].split()[2:5]
#print date, time, ampm
m, d, y = [int(v) for v in date.split('/')]
H, M = [int(v) for v in time.split(':')]
if ampm.lower() == 'pm':
H += 12
now = datetime.datetime.now()
then = datetime.datetime(y, m, d, H, M)
diff = now - then
return diff
@hook.command
def uptime_win(inp,say=None):
if __name__ == '__main__':
say(uptime())
它没有给我一个错误,但它没有显示。我查看了代码,我不明白为什么我看不到它。也许它可能很小但我看不到它:D。我包含了所需的模块,但它仍然无法正常工作:'(。另外我想问一下你们是否有更简单的方法来获得 Windows 的正常运行时间(我已经有 linux)。谢谢!