首页 > 编程语言 > 详细

Python中的NTP对时

时间:2021-07-30 16:03:20      阅读:22      评论:0      收藏:0      [点我收藏+]

 1. 首先需要安装ntplib模块

 pip install ntplib 

2.windows中获取系统时间及对时

1 # 获取系统日期(修改把/t改为日期字符串)
2 date /t
3 # 获取系统时间(修改把/t改为时间字符串)
4 time /t

3.NTP对时的函数示例代码

 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对时失败)

 

Python中的NTP对时

原文:https://www.cnblogs.com/wqm94/p/15078754.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!