Git Product home page Git Product logo

Comments (8)

cf-natali avatar cf-natali commented on August 30, 2024

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.

nschloe avatar nschloe commented on August 30, 2024
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.

cf-natali avatar cf-natali commented on August 30, 2024

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.

mahtin avatar mahtin commented on August 30, 2024

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.

cf-natali avatar cf-natali commented on August 30, 2024

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.

cf-natali avatar cf-natali commented on August 30, 2024

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.

mahtin avatar mahtin commented on August 30, 2024

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.

cf-natali avatar cf-natali commented on August 30, 2024

No worries then - it's unfortunately easy to mis-communicate online.

Cheers!

from ntplib.

Related Issues (17)

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.