码迷,mamicode.com
首页 > 其他好文 > 详细

时间同步工具

时间:2015-07-09 09:34:55      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

如果你的电脑时间老是不对,可以用下面的脚本,放任务管理器里面运行。
import os
import time
import ntplib
def SyncTime(ntpserv):
try:
c = ntplib.NTPClient()
print ‘Synctime from %s ‘ % ntpserv
response = c.request(ntpserv)
ts = response.tx_time
except ntplib.NTPException,e:
print ‘Synctime Failed,‘,e
return False
else:
_date = time.strftime(‘%Y-%m-%d‘,time.localtime(ts))
_time = time.strftime(‘%X‘,time.localtime(ts))
os.system(‘date {} && time {}‘.format(_date,_time))
print ‘Synctime Success!‘
return True

if __name__ == ‘__main__‘:
with open(‘ntpserv.ini‘) as f:
for eachline in f:
ntpserv = eachline.strip(‘\n\r‘)
if SyncTime(ntpserv):
break

时间同步工具

标签:

原文地址:http://www.cnblogs.com/xiewb/p/4632133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!