write DBUS Services (II)

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/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值