我正在写一个pydbus服务,我已经注册了一个接口,并提供了一个方法,如
import pydbus
from pydbus import SessionBus
from gi.repository import GLib
class Server():
"""
<node>
<interface name="org.freedesktop.testSrv">
<method name="test">
<arg direction="in" type="s" name="testarg"/>
</method>
</interface>
</node>
"""
def test(testarg):
# here i want to access the sender id
print(testarg)
bus = SessionBus()
bus.publish("org.freedesktop.testSrv", Server())
loop = GLib.MainLoop()
loop.run()
当一个客户端调用这个端点的方法时,我想知道原点(sender),它只是执行函数test()。
我知道dbus在请求中提供了这个信息,比如:.1.23,但是我没有找到用python访问这个信息的解决方案。
你可以在你的方法签名中包含一个MethodCallContext(它是一个命名的参数,所以必须是 dbus_context
):
def test(self, testarg, dbus_context):
print(f'"{testarg}" received from {dbus_context.sender}')
我不知道这是否被认为是pydbus的一个实现细节或者是API的一部分。