write DBUS Services (II)

被刻印的时光 ゝ 提交于 2019-12-06 07:42:42

For the system bus server you would need only to modify line 13 from the sessionbus server and line 7 from the client scripts; to replace SessionBus() with SystemBus(). Let’s also change the names for the methods and for the service.

The final code would look like this:


#!/usr/bin/env python3

from gi.repository import GLib
import dbus
import dbus.service

from dbus.mainloop.glib import DBusGMainLoop

class Session_DBus(dbus.service.Object):
    def __init__(self):
        bus_name=dbus.service.BusName('org.me.test_system',bus=dbus.SystemBus())
        dbus.service.Object.__init__(self,bus_name,'/org/me/test_system')

    @dbus.service.method('org.me.test1')
    def session_bus_message1(sefl):
        return "Session Bus 1"

    @dbus.service.method('org.me.test2')
    def session_bus_message2(self):
        return "Session Bus 2"

    @dbus.service.method('org.me.test2')
    def session_bus_strings(self,string1,string2):
        return string1+" "+string2



DBusGMainLoop(set_as_default=True)
dbus_service=Session_DBus()

try:
    GLib.MainLoop().run()
except KeyboardInterrupt:
    print("\nThe MainLoop will close...")
    GLib.MainLoop().quit()

And for the clients it would look something like this:
Vers1

#!/usr/bin/env python3

import dbus
bus = dbus.SystemBus()
session = bus.get_object("org.me.test_system", "/org/me/test_system")

method_message1 = session.get_dbus_method('session_bus_message1', 'org.me.test1')
method_message2 = session.get_dbus_method('session_bus_message2', 'org.me.test2')
method_message3 = session.get_dbus_method('session_bus_strings', 'org.me.test2')

print(method_message1())
#interface1 = dbus.Interface(session, "org.me.test1")
#print(interface1.session_bus_message1())
print(method_message2())
print(method_message3("Hello", "World"))
Vers2:
#!/usr/bin/env python3

import dbus
bus = dbus.SystemBus()
session = bus.get_object("org.me.test_system", "/org/me/test_system")
interface1 = dbus.Interface(session, "org.me.test1")
interface2 = dbus.Interface(session, "org.me.test2")

print(interface1.session_bus_message1())
print(interface2.session_bus_message2())
print(interface2.session_bus_strings("hello", "world"))

Of course, if you try the same thing now: run the server and then test the clients you would see that you get an error after trying to turn on the server: a message saying that a specific connection is not allowed to own a service. This thing is because (remember from the first lines of the post) you try (as a regular user) to run a D-Bus that may change the entire system state.

To run the server and test the clients you could to the following:

  1. Create a file in /etc/dbus-1/system.d/ with the name org.me.test_system.conf with the following content:
<!DOCTYPE busconfig PUBLIC                                                      
 "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"                         
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">                 
                                                                                  
<busconfig>                                                                     
    <!-- Owned only by the root -->                                            
    <policy user="root">                                                        
        <allow own="*"/>                                                        
    </policy>                                                                   
                                                                                  
    <!-- What to allow for users -->                                           
    <policy context="default">                                                  
        <allow send_destination="*"/>                                           
        <allow send_interface="*"/>                                             
    </policy>  

  1. Run the server and then you can test the clients :).

Also if you want to only to run the clients (the server to automatically kick in) you can create a file in /usr/share/dbus-1/system-services/ with the name org.me.test_system.service that would contain the following lines:

1
2
3
4
[D-BUS Service]
Name=org.me.test_system
Exec=server_location
User=root

That is all for now and I hope you found this tutorial helping :D.

“In vain have you acquired knowledge if you have not imparted it to others.”
Deuteronomy Rabbah

Have a great day,
George

https://georgemuraruc.wordpress.com/2015/07/16/d-bus-tutorial-for-python/
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!