Update NEWS for 0.81.0
[dbus-python-phuang.git] / examples / list-system-services.py
blob5cf619fe4a0d722321b4a355115d064208091760
1 #!/usr/bin/env python
3 """Usage: python list-system-services.py [--session|--system]
4 List services on the system bus (default) or the session bus."""
6 import sys
8 import dbus
10 def main(argv):
11 factory = dbus.SystemBus
13 if len(argv) > 2:
14 sys.exit(__doc__)
15 elif len(argv) == 2:
16 if argv[1] == '--session':
17 factory = dbus.SessionBus
18 elif argv[1] != '--system':
19 sys.exit(__doc__)
21 # Get a connection to the system or session bus as appropriate
22 # We're only using blocking calls, so don't actually need a main loop here
23 bus = factory()
25 # This could be done by calling bus.list_names(), but here's
26 # more or less what that means:
28 # Get a reference to the desktop bus' standard object, denoted
29 # by the path /org/freedesktop/DBus.
30 dbus_object = bus.get_object('org.freedesktop.DBus',
31 '/org/freedesktop/DBus')
33 # The object /org/freedesktop/DBus
34 # implements the 'org.freedesktop.DBus' interface
35 dbus_iface = dbus.Interface(dbus_object, 'org.freedesktop.DBus')
37 # One of the member functions in the org.freedesktop.DBus interface
38 # is ListNames(), which provides a list of all the other services
39 # registered on this bus. Call it, and print the list.
40 services = dbus_iface.ListNames()
41 services.sort()
42 for service in services:
43 print service
45 if __name__ == '__main__':
46 main(sys.argv)