Skip to main content

Modern, type-safe, zero-dependency python library for implementing a network time protocol (NTP) stratum 0 server

Project description

ntps

Modern, type-safe, zero-dependency python library for implementing a network time protocol (NTP) stratum 0 server.

Installation

pip install ntps

or

using your preferred environment / package manager of choice, e.g., poetry, conda or uv:

poetry add ntps
conda install ntps
uv add ntps

Usage

There are two main components in the ntps library: the NTP server and the NTP client.

The NTP server is used to create an NTP server that can be used to synchronize time across a network. The NTP client is used to query an NTP server for the current time.

The following is an example of how to use the ntps library to create an NTP server and client:

Server

from asyncio import get_running_loop, run
from time import time

from ntps import NTPServer

# Define a custom NTP server that uses GPS-synced time as the reference time, and a
# stratum level of 0:
class GNSSStratum0NTPServer(NTPServer):
    # Set the reference identifier of the NTP server:
    refid = "GPS"
    # Set the stratum level of the NTP server:
    stratum = 0

    def get_ntp_time(self) -> float:
        # Replace this with your actual GPS-synced time retrieval:
        return time()


async def main() -> None:
    # Retrieve the current running asynchronous event loop:
    loop = get_running_loop()

    # Create a UDP server endpoint on all interfaces at port 123 using your
    # custom implementation (e.g., GNSSStratum0NTPServer):
    transport, _ = await loop.create_datagram_endpoint(
        lambda: GNSSStratum0NTPServer(),
        local_addr=('0.0.0.0', 123),
    )

Client

from ntps import NTPClient, NTPPacket

async def main() -> None:
    # Create a new NTP client instance:
    client = NTPClient(endpoint="0.0.0.0", port=123)

    # Send an NTP request to the specified NTP server (e.g., 0.pool.ntp.org):
    packet: NTPPacket = client.query()

    # Print the response packet from the NTP server:
    print(packet)

As the ntps instance is fully typed, you can use your IDE's autocompletion to see all the available methods and properties.

We have also provided further usage examples in the examples directory.

Milestones

  • Implement NTP server
  • Implement NTP client
  • Implement NTP packet
  • Implement kiss-of-death (KoD) packet
  • Implement NTP authentication
  • Implement NTP broadcast
  • Implement NTP multicast
  • Implement NTP symmetric mode

Miscellaneous

For more information on the NTP protocol, please refer to the RFC 5905 document.

License

This project is licensed under the terms of the MIT license.

Project details


Download files

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

Source Distribution

ntps-0.2.0.tar.gz (31.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ntps-0.2.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file ntps-0.2.0.tar.gz.

File metadata

  • Download URL: ntps-0.2.0.tar.gz
  • Upload date:
  • Size: 31.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ntps-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a02c9f3efed59d7f9db3f40e83cb7cdf2dbd8ef71a3d359e61b3c47712eca476
MD5 03ee5761960a12ef363d2125a8b98f57
BLAKE2b-256 55e24f03e7aa6434a83b35bba6897555d158bc51ee758e40648f7c03b6561ece

See more details on using hashes here.

Provenance

The following attestation bundles were made for ntps-0.2.0.tar.gz:

Publisher: publish.yml on michealroberts/ntps

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ntps-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: ntps-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ntps-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5e5c643dfb3f31c0961e09368da3d8728004cde4c128d4909bf8bff409f237d3
MD5 b2c8b60e18a94fcb596a0c528f267168
BLAKE2b-256 3b6e2fd07f0b517d4caf532fda9fbb41252c161b5fc2630ddda693e815b60c0a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ntps-0.2.0-py3-none-any.whl:

Publisher: publish.yml on michealroberts/ntps

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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