Check NTP synchronization status from Python


#1

Does anyone have an example of how to query the NTP synchronization state from Python using the dbus library?

I can get it to work from the command line using the instructions here, but I’m struggling to work out how to do this from the Python library.


#2

Hey, it takes a bit of trial and error to work with dbus, but it’s not too bad after a while :slight_smile:

Here’s a minimal example:

import python
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.timedate1',
                       '/org/freedesktop/timedate1')
interface = dbus.Interface(proxy, 'org.freedesktop.DBus.Properties')
properties = interface.GetAll('org.freedesktop.timedate1')
print(properties)

If you are interested, here’s an expanded example (to show some dbus type conversions and suchalike), but this above should be enough to get started.

Please note that this example is in our “playground” so it’s not guaranteed to work all the time (I’ve just tested it now).


#3

Super - many thanks.