NTP (Network Time Protocol) was proposed by Professor David L. Mills of the University of Delaware in 1985. It is a communication protocol designed to enable different machines to maintain the same time on the Internet.
NTP estimates the round-trip delay of packets on the network and independently estimates the computer clock deviation, thereby achieving high-precision computer timing on the network.
NTP service is relatively common in Linux systems. In fact, the same is true for Python. When you search for "python to get time" online, many of them are clumsy ways to parse the page to get the time. However, Python can also use the NTP service for time synchronization to obtain accurate time. You only need to use the ntplib library to achieve this.
Introduction to the usage of ntplib library
Install ntplib:
easy_install ntplib
or
pip install ntplib
The code is below.
import os import time import ntplib c = ntplib.NTPClient() response = c.request('pool.ntp.org') ts = response.tx_time _date = time.strftime('%Y-%m-%d',time.localtime(ts)) _time = time.strftime('%X',time.localtime(ts)) os.system('date {} && time {}'.format(_date,_time))
This way you can easily synchronize local time~