Git Product home page Git Product logo

ntplib's People

Contributors

cf-natali avatar dforsi avatar dopstar avatar jnmeurisse avatar mahtin avatar mgorny avatar nradonicich avatar programadorhedonista avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ntplib's Issues

Formatting error when stratum = 0

stratum_to_text returns NTP.STRATUM_TABLE[stratum] % stratum but STRATUM_TABLE[0] (="unspecified or invalid") does not contain any format specifier.

How to get the NTP Synchronized status only ?

I want to get the NTP Synchronized status by this api .

l want to find a command that can show the machine readable attribution for which is like a constant string or number no matter in other Debian version .

My linux version : Debian 9

image

PermissionError: [Errno 1] Operation not permitted

Can anybody help me ?

>>> import ntplib
>>> from time import ctime
>>> c = ntplib.NTPClient()
>>> response = c.request('ntp.aliyun.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/kspython3.7/lib/python3.7/site-packages/ntplib.py", line 313, in request
    s.sendto(query_packet.to_data(), sockaddr)
PermissionError: [Errno 1] Operation not permitted
>>> response = c.request('ntp.aliyun.com', version=3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/kspython3.7/lib/python3.7/site-packages/ntplib.py", line 313, in request
    s.sendto(query_packet.to_data(), sockaddr)
PermissionError: [Errno 1] Operation not permitted

Bug due to float imprecision

When doing an NTP server,

if we receive a NTP request from a client, the byte data is converted to a float by the NTP_Packet::from_data. More precisely it reassemble the tx_timestamp_high and low into a float

However, when we need to send back the orig_timestamp to the customers, it convert back the float to orig_timestamp_high and low.

However there is a lost of precision and the timestamp are not totally identicals, so if the client compare the 2 times (the one transmit and the one send back by the servers, that should be equals), it'll reject the update.
image

A fix was done in this repo : https://github.com/limifly/ntpserver/blob/master/ntpserver.py#L186 by adding new variables (to avoid float conversion)

Best regards.

Google Colab: gaierror: [Errno -8] Servname not supported for ai_socktype

When running

import ntplib
c = ntplib.NTPClient()
c.request("pool.ntp.org", version=3)

on Google Colab (a Jupyter Python env), I'm getting

---------------------------------------------------------------------------
gaierror                                  Traceback (most recent call last)
[<ipython-input-5-f665254241c7>](https://localhost:8080/#) in <cell line: 3>()
      1 import ntplib
      2 c = ntplib.NTPClient()
----> 3 c.request("pool.ntp.org", version=3)

1 frames
[/usr/lib/python3.10/socket.py](https://localhost:8080/#) in getaddrinfo(host, port, family, type, proto, flags)
    953     # and socket type values to enum constants.
    954     addrlist = []
--> 955     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    956         af, socktype, proto, canonname, sa = res
    957         addrlist.append((_intenum_converter(af, AddressFamily),

gaierror: [Errno -8] Servname not supported for ai_socktype

Any idea why that may be?

Randomize the addresses

Presently the getaddrinfo code always uses the first returned value. For NTP pools this could be improved such that more than just the first entry is used.

>>> import socket
>>> 
>>> host = 'europe.pool.ntp.org'
>>> port = 'ntp'
>>> address_family=socket.AF_UNSPEC
>>> addrinfo = socket.getaddrinfo(host, port, address_family)
>>> for x in addrinfo:
...   print( x[0], x[1], x[4] )
... 
AddressFamily.AF_INET SocketKind.SOCK_STREAM ('80.209.111.50', 123)
AddressFamily.AF_INET SocketKind.SOCK_STREAM ('217.14.146.53', 123)
AddressFamily.AF_INET SocketKind.SOCK_DGRAM ('81.21.76.27', 123)
AddressFamily.AF_INET SocketKind.SOCK_STREAM ('81.21.76.27', 123)
AddressFamily.AF_INET SocketKind.SOCK_DGRAM ('94.158.46.150', 123)
AddressFamily.AF_INET SocketKind.SOCK_STREAM ('94.158.46.150', 123)
AddressFamily.AF_INET SocketKind.SOCK_DGRAM ('80.209.111.50', 123)
AddressFamily.AF_INET SocketKind.SOCK_DGRAM ('217.14.146.53', 123)
>>> 

The current code just uses [0]. Could I recommend picking this randomly.

>>> import random
>>> print(addrinfo[random.randrange(0,len(addrinfo))])
(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('87.233.197.123', 123))

The resulting code would be:

...
import random
...
        # lookup server address
        addrinfo_a = socket.getaddrinfo(host, port, address_family)
        addrinfo = addrinfo_a[random.randrange(0,len(addrinfo_a))]
        family, sockaddr = addrinfo[0], addrinfo[4]
...

If only one address exists this code still works.

`ntplib.ref_id_to_text(response.ref_id)` is not working correctly

hi, i am using ntplib-0.4.0 and wrote a little python3 script to 'scrape' the stratum one server from the list of ntp.org StratumOneTimeServers
and test the listed hostnames with ntplib if they respond and 'identify' as statum 1 servers.

i have the issue that ntplib.ref_id_to_text(response.ref_id) always gives me an ip like dotted representation instead of a 4 letter word.
so PPS is translated to 80.83.83.0 and some other of the ref_id's that could be 4 letter words.

see the attached CSV filethe column ref_id was filled with the result of ntplib.ref_id_to_text(response.ref_id):
issue_ref_id.csv

the construct ntplib.ref_id_to_text(response.ref_id) i took from the pip example code snip

Don't delete old versions on pypi

version 0.3.3 got deleted on pypi. That makes it difficult for eg. packagers where the package checks the local (packaged) version with the upstream version on pypi

Very inaccurate client fails to request NTP data

When the client thinks the current time is after the year 2037, the library fails to make an NTP request.

This can be easily demonstrated using the faketime library:

$ faketime '2037-01-01 00:00:00' python3 -c 'import ntplib; ntplib.NTPClient().request("0.pool.ntp.org")'
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/ntplib.py", line 170, in to_data
    packed = struct.pack(
struct.error: 'I' format requires 0 <= number <= 4294967295

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/ntplib.py", line 313, in request
    s.sendto(query_packet.to_data(), sockaddr)
  File "/usr/local/lib/python3.10/site-packages/ntplib.py", line 189, in to_data
    raise NTPException("Invalid NTP packet fields.")
ntplib.NTPException: Invalid NTP packet fields.

Before the year 2036 the above succeeds as expected. Tested on the python:3.10 docker image with ntplib 0.4.0. I'm not sure if the problem is just due to the ~15 year inaccuracy, or this version of the library will stop working at some point in 2036.

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.