Python uses the Ntplib library to synchronize local time calibration methods _python

Source: Internet
Author: User
Tags local time

NTP (Network time Protocol) was proposed in 1985 by Professor David L. Mills of the University of the United States to design a communication protocol designed to enable different machines to maintain the same time on the Internet.

NTP estimates the round-trip latency of packets on the network, and independently estimates the computer clock bias to achieve high precision computer school on the network.

NTP service in Linux system is more common, in fact, Python is the same, can be online search "Python acquisition Time", many are the stupid way to parse the page to get time, but Python can also use NTP services to achieve time synchronization to obtain accurate time, You only need to use the Ntplib library to implement it.

Introduction to Ntplib Library usage

Install Ntplib:

Easy_install Ntplib

Or

Pip Install Ntplib

The code 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 will make it easy to sync local time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.