Home > Backend Development > Python Tutorial > Python uses the ntplib library to synchronize local time calibration

Python uses the ntplib library to synchronize local time calibration

WBOY
Release: 2016-07-22 08:56:35
Original
2095 people have browsed it

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
Copy after login

or

pip install ntplib
Copy after login

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)) 

Copy after login

This way you can easily synchronize local time~

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template