write DBUS Services (II)
2017-04-09 10:32
183 查看
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:
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:
Create a file in /etc/dbus-1/system.d/ with the name org.me.test_system.conf with the following content:
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:
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/
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:
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>
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:
“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/
相关文章推荐
- write DBUS Services (I)
- XLSMini控件是XLSReadWriteII的简版可读写Excel单元格的值
- 安装libpcap错误 ./pcap-dbus.c: In function ‘dbus_write’:已解决
- [iOS/Android开发之WebService]How to write a webservices using php with json format
- How to troubleshoot the “Red Arrow” issue in Component Services (II)
- 分享一下解决 XLSReadWriteII 解析Excel95格式的文件乱码的问题
- Search a 2D Matrix IIWrite an efficient algorithm that searches for a value in an m x n matrix. This
- Webgoat 笔记总结 Web Services II
- XLSReadWriteII控件来完成10×10的乘法表
- 转 XlsReadWriteII 的博文(自留参考)
- Delphi中使用XlsReadWriteII
- How to write RESTful web services using spring 3 mvc
- 如何使用XlsReadWriteII在Delphi中读取Excel文件
- Excel读写方案XLSReadWriteII使用技巧总结
- TXLSReadWriteII读和写Excel
- DBus services
- 支持.NET和移动设备的XLS读写控件XLSReadWriteII下载地址及介绍
- XLSReadWriteII 使用
- Tomcat 5.5.15中自定义Services的配置
- PostgreSQL数据库集群:pgpool-II工具