如何获取发件人(客户端进程),当 运行 pydbus 服务时

How to get the Sender(client process), when running a pydbus service

我正在写一个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()

当客户端调用这个端点的方法时,我想知道来源(发送者),它只是执行函数 test()。

我知道 dbus 在请求中提供了这个,例如。喜欢 :.1.23 但我没有找到使用 python 访问此信息的解决方案。

您可以在方法签名中包含 MethodCallContext(这是一个命名参数,因此必须 dbus_context):

  def test(self, testarg, dbus_context):
    print(f'"{testarg}" received from {dbus_context.sender}')

我不知道这是 pydbus 的实现细节还是 API 的一部分。