ntplib同步时间

#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年前

评论

评论已关闭

小小英雄杜恩

子曰:“弟子入则孝,出则弟,谨而信,泛爱众,而亲仁。行有余力,则以学文。”


:D 获取中...