pip install ntplib
1 # 获取系统日期(修改把/t改为日期字符串) 2 date /t 3 # 获取系统时间(修改把/t改为时间字符串) 4 time /t
1 def ntp_timing(): 2 # 以阿里云NTP对时服务器为例 3 response = ntplib.NTPClient().request('ntp.aliyun.com') 4 ts = response.tx_time 5 _date = time.strftime('%Y-%m-%d', time.localtime(ts)) 6 _time = time.strftime('%X', time.localtime(ts)) 7 os.system('date {} && time {}'.format(_date, _time)) 8 t1 = time.strftime('%Y-%m-%d %X', time.localtime(ts)) 9 t2 = datetime.datetime.now().strftime('%Y-%m-%d %X') 10 if t1 == t2: 11 print('NTP对时成功') 12 else: 13 print('NTP对时失败')