用户:
气泡鱼要上首页OoOo查看:2 回复:7 评论:2 创建时间:2020-08-28T15:02:46
大佬们怎么同!步!时!间!
JSR_白鸾超级复杂滴~~~~
用Python?
得安装ntplib。(一个库)
局域网内,利用另一台服务器,穿越UDP代理,与互联网时间同步。
前部分用于win7系统,后部分用于win2008。
需要 pip3 install ntplib
需要启动Windows Time服务。
然后,就可以写代码了!
import os
import time
import ntplib
def SetClockL():
try:
c = ntplib.NTPClient()
resp喵e = c.request('192.168.217.5') #你的IP地址。自己改
ts = resp喵e.tx_time
_date = time.strftime('%Y-%m-%d', time.localtime(ts))
_time = time.strftime('%X', time.localtime(ts))
os.system('date {} && time {}'.format(_date, _time))
except:
print("SetClockL Get Error!")
else:
print("Set System OK!" + 'date {} && time {}'.format(_date, _time))
print("\n")
print('LocalTime old: ' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
SetClockL()
print('LocalTime new: ' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print("\n")
import datetime
import ntplib
import time
import win32api
def SetClock(): #
try:
client = ntplib.NTPClient()
resp喵e = client.request('ntp.api.bz')
print(datetime.datetime.fromtimestamp(resp喵e.tx_time))
# print(time.gmtime(resp喵e.tx_time))
tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst = time.gmtime(
resp喵e.tx_time)
win32api.SetSystemTime(tm_year, tm_mon, tm_wday,
tm_mday, tm_hour, tm_min, tm_sec, 0)
print("SetSystemTime OK!")
except:
print("SetClock Get Error!")
else:
print('Local Time new: ' +
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
SetClock()
a = input("\n按回车退出!\n")
有可能会有bug。
你试试吧
点赞0
评论