我正在尝试在 python 中访问文件描述符。
此外,我使用 gdbus-codegen 生成服务器端绑定和访问方法,然后从 python 客户端。
基本上调用方法,通过 GVariant 类型传递普通参数并获取 python 中的返回值。
我的申请围绕:
https://github.com/GNOME/glib/blob/master/gio/tests/gdbus-example-server.c
调用方法
GimmeStdout
我看不到简单地作为返回类型返回的文件描述符。文件描述符作为 GUnixFDList
返回,我不知道如何在 python 中访问它。
我尝试使用自己的测试方法和文件文件描述符,但它们不是
stdout
所以问题是:如何在Python中访问通过DBus传输的UNIX_FD?
假设您使用 https://github.com/GNOME/pygobject 作为 Python D-Bus 库,那么为了获取通过 D-Bus 消息传递的文件描述符,您应该执行以下操作:
from gi.repository import Gio
def on_method_return(conn, result, userdata):
reply = conn.call_with_unix_fd_list_finish(result)
print(reply.out_fd_list) # UnixFDList returned by the server
conn = Gio.bus_get_sync(Gio.BusType.SESSION, None)
conn.call("org.service", "/", "org.gtk.GDBus.TestInterface", "GimmeStdout",
None, None, Gio.DBusCallFlags.NONE, -1, None, on_method_return, None)
如果您想将文件描述符发送到某些 D-Bus 服务器,您需要调用
conn.call()
,而不是 conn.call_with_unix_fd_list()
。对于同步调用,请使用 *_sync()
调用版本。