Skip to main content

No project description provided

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

python_ntp-1.0.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file python_ntp-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: python_ntp-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.10

File hashes

Hashes for python_ntp-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2510c5f784e80ed8c725c73b015504ccc7c3e2b009d5d80932b475493f5eccd
MD5 967e1c446d00edb8ddcb3ef9d28cb81d
BLAKE2b-256 078a207e29565f905558aa897e5283040fb01a57978c531198f9da7d9a0b736b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page