Comments (8)
Looks like an issue with service resolution.
What does the following return in your environment?
In [5]: socket.getaddrinfo("pool.ntp.org", "ntp", socket.AF_UNSPEC)
And
In [6]: socket.getservbyname("ntp")
from ntplib.
import socket
socket.getaddrinfo("pool.ntp.org", "ntp", socket.AF_UNSPEC)
gives
---------------------------------------------------------------------------
gaierror Traceback (most recent call last)
[<ipython-input-2-50891d166737>](https://localhost:8080/#) in <cell line: 3>()
1 import socket
2
----> 3 socket.getaddrinfo("pool.ntp.org", "ntp", socket.AF_UNSPEC)
[/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
from ntplib.
See above, what about?
socket.getservbyname("ntp")
But in any case looks like your environment cannot resolve the "ntp" services - you could try passing the port explicitly:
c.request("pool.ntp.org", version=3, port=123)
from ntplib.
You can replicate this error by simply removing the ntp
entry from /etc/services
file. Which makes sense. When there is no ntp
entry; then the code fails; but is kinda ugly. As port 123 was defined nearly 38 years ago as port 123
, it's safe to say that hardcoding it into the code is safe. See:
$ git diff
diff --git a/ntplib.py b/ntplib.py
index 77ae7b7..6f4049e 100644
--- a/ntplib.py
+++ b/ntplib.py
@@ -298,7 +298,11 @@ class NTPClient(object):
"""
# lookup server address
- addrinfo = socket.getaddrinfo(host, port, address_family)[0]
+ try:
+ addrinfo = socket.getaddrinfo(host, port, address_family)[0]
+ except socket.gaierror:
+ # 123 is defined in Sept/1985 by https://www.rfc-editor.org/rfc/rfc958
+ addrinfo = socket.getaddrinfo(host, 123, address_family)[0]
family, sockaddr = addrinfo[0], addrinfo[4]
# create the socket
$
Restore /etc/services
after performing this test! Seriously!
from ntplib.
Yes, see above, the request
method supports passing the port explicitly.
It's the first time in over 10 years since this library exists I see an environment which doesn't define the ntp
service.
from ntplib.
I've changed the code to use port 123 instead of "ntp" to avoid issues in such environments.
@mahtin
I do not appreciate your tone.
The issue was obvious, which is why I asked the OP to test with socket.getservbyname("ntp")
before making the change.
Your snarky comments do not bring anything to the conversation, so please refrain from them.
from ntplib.
An absolute apology for anything misunderstood - I was in no way meaning to be snarky. If you meant when I said "but is kinda ugly", then trust me that that's a reference to the underlying libraries socket.gaierror
name/error value, not your code. Your code, as others also believe, is damn useful and excellent code.
Plus, as you said, services not resolving is a very rare issue.
Apologies again.
from ntplib.
No worries then - it's unfortunately easy to mis-communicate online.
Cheers!
from ntplib.
Related Issues (17)
- Where can we install python2-ntplib 0.3.3 now? HOT 7
- Formatting error when stratum = 0 HOT 1
- PermissionError: [Errno 1] Operation not permitted HOT 1
- Very inaccurate client fails to request NTP data HOT 5
- Could you please push tag 0.3.4? HOT 1
- async support? HOT 3
- proxy support? HOT 1
- Randomize the addresses HOT 1
- Stratum 0 fails stratum_to_text function HOT 1
- append nts HOT 1
- `ntplib.ref_id_to_text(response.ref_id)` is not working correctly HOT 2
- Bug due to float imprecision
- Syncing the system time HOT 1
- How to get the NTP Synchronized status only ? HOT 1
- Don't delete old versions on pypi HOT 1
- Please upload source tarballs to pypi in addition to wheels HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ntplib.