Coder Social home page Coder Social logo

python-ntp's Introduction

python-ntp

Pure python, clean room implementation of NTP client based directly on RFC5905.

  • No extra dependencies, it is a single source file that works in vanilla Python 3.5 or newer.
  • Tested to work on Linux, macOS and Windows.
  • Any number of NTP servers can be used.
  • NTP servers v4 and v3 are supported.
  • And so are IPv4 and IPv6.
  • The full suite of client side algorithms is implemented. From periodic polling of each peer, to finding consensus, to aggregate offset and jitter. Except the logic of correcting the local time, as this was never the goal. It still can be used to correct local system time, see the examples below.
  • CLI offers functionality similar to ntpdate(8), again, except the logic of setting the local time which can be worked around.

API

Intended use is a thread running in a loop:

from time import sleep
from ntp import NtpArena

# IPv4 of IPv6 addresses must be fed into NtpArena, hostnames must be resolved first.
ntp = NtpArena(addresses=["217.114.59.66", "82.69.171.134"])

while True:
    pause = ntp.query_peers()
    leap, offset, jitter = ntp.calculate_state()
    print(offset)
    sleep(pause)

Lower level API is exposed via NtpMessage and NtpAssociation classes.

CLI

Keep running and showing NTP time every minute:

ntp.py --output-interval 60 pool.ntp.org time.nist.gov time.google.com

One shot run, returning offset between local and NTP time:

ntp.py --output-count 1 --output-format '{offset:+}' pool.ntp.org

Feeding the output to date(1), in order to set the system time. This is crude PLL logic, as in the clock discipline algorithm.

date --set="$(ntp.py --output-interval 30 --output-count 1 pool.ntp.org)"

Use ntp.py --help to learn more about available options.

python-ntp's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.