Python uses the ntplib library to synchronously calibrate the local time, pythonntplib

Source: Internet
Author: User

Python uses the ntplib library to synchronously calibrate the local time, pythonntplib

Network Time Protocol was proposed by Professor David L. Mills of the University of Drava in 1985 to design a Protocol for enabling 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.