如何在运行pydbus服务时获取发送方(客户端进程)

2024-04-28 21:53:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个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访问这些信息的解决方案


Tags: namefromorgtestimportnodeservermethod
1条回答
网友
1楼 · 发布于 2024-04-28 21:53:03

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

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

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

相关问题 更多 >