正确设置时间的脚本

2024-09-23 04:25:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一个快速的python脚本,它在系统启动后运行。基本上,我的CMOS电池坏了,我现在买不到新的。所以我想制作一个脚本,在线检查日期和时间,然后通过它设置系统时间。我在网上找过类似的剧本,但没有看到。这在python中是可能的吗?我该怎么做呢?谢谢!在


Tags: 脚本电池系统时间cmos剧本
2条回答

此python代码将帮助您:

import os

os.environ['TZ'] = 'America/Montevideo'  # very important to set your time zone

os.system('ntpdate -s  south-america.pool.ntp.org')
print 'Time changed to: '
os.system('date')

您可以使用restwebservice(Python有json api,但您可以简单地使用'requests'libs)

import requests
from requests_kerberos import HTTPKerberosAuth

r = requests.get('http://timeapi.org/utc/now.json?callback=myCallback', auth=('user', 'pass')

data = r.json()

对于这里的web服务(感谢http://www.timeapi.org/): http://timeapi.org/utc/now.json?callback=myCallback

相关问题 更多 >