Python uses the ntplib library to synchronously calibrate local time

Source: Internet
Author: User
The NTP Network Time Protocol can be accessed more or less at ordinary times. many Internet time synchronization functions in operating systems, including Windows, are implemented based on NTP, here, let's take a look at Python's method of synchronizing calibration of local Time using the ntplib Library NTP (Network Time Protocol) is by David L of the University of Drava. professor Mills proposed in 1985 that a communication protocol was designed to allow 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 deviation, so as to achieve high precision during computer calibration on the network.

NTP is common in Linux systems. In fact, Python is the same. when you can search for "python get time" on the Internet, many of them are stupid ways to parse the page get time, python can also use the NTP service for time synchronization to obtain the exact time. you only need to use the ntplib Library.

Usage of ntplib Library

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

In this way, you can easily synchronize local time ~

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.