Python中的NTP对时
2021/7/30 11:35:56
本文主要是介绍Python中的NTP对时,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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对时的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python