cf-natali / ntplib Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Can ntplib run asynchronously? not block coroutine.
stratum_to_text returns NTP.STRATUM_TABLE[stratum] % stratum
but STRATUM_TABLE[0] (="unspecified or invalid") does not contain any format specifier.
Today, I found that I can't install ntplib in python2.
All the old versions are removed?
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
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.
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.
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?
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.
is it possible to get the time through a proxy?
append nts
For packagers (eg. rpm or deb), the sources are used for creating distro packages. Please provide the source tar.gz file on pypi
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
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
Line 456 in 08d0f7e
When stratum is 0, the function fails with:
TypeError: not all arguments converted during string formatting
This is due to the assumption that stratum will always be 1
Could you please push tag 0.3.4 to have a link to the actual 0.3.4 version of lib in case of future updates?
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.
Hi, is there an option to sync system time with this library?
Thanks for answer
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.