linux使用python脚本同步日期时间

Posted by Tesla9527 on August 30, 2021

安装ntplib包

1
pip install ntplib

新建python脚本并执行

1
2
3
4
5
6
7
8
9
10
11
12
import time
import os

try:
    import ntplib
    client = ntplib.NTPClient()
    response = client.request('pool.ntp.org')
    os.system('date ' + time.strftime('%m%d%H%M%Y.%S',time.localtime(response.tx_time)))
except:
    print('Could not sync with time server.')

print('Done.')