#coding:utf-8
import ntplib,os
from time import sleep,strftime,localtime
from datetime import datetime
def synchronize_ntpserver_time(ntserver):
ntp_client = ntplib.NTPClient()
try:
ntp_stats = ntp_client.request(ntserver)
fmt_time = strftime('%X', localtime(ntp_stats.tx_time + 0.1))
fmt_data = strftime(('%Y-%m-%d'), localtime(ntp_stats.tx_time))
os.system(f'date {fmt_data}')
os.system(f'time {fmt_time}')
print(fmt_data, fmt_time)
return True
except:
return False
if __name__=='__main__':
synchronize_ntpserver_time('ntp.ytpgt.com')
文档最后编辑于3年前
评论已关闭