通过TCP连接到dbus [英] Connecting to dbus over tcp

查看:352
本文介绍了通过TCP连接到dbus的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的python程序来播放和暂停女妖音乐播放器。
当它在我自己的机器上工作时,我很难将其连接到连接到同一路由器(LAN)的远程计算机。
我编辑了远程计算机的session.conf,以添加以下行:

 < listen> tcp: host = localhost,port = 12434< / listen> 

这是我的程序:

 导入dbus 


bus_obj = dbus.bus.BusConnection( tcp:host = localhost,port = 12434)
proxy_object = bus_obj .get_object('org.bansheeproject.Banshee',
'/ org / bansheeproject / Banshee / PlayerEngine')

playerengine_iface = dbus.Interface(proxy_object,
dbus_interface ='org .bansheeproject.Banshee.PlayerEngine')

var = 0

而(var!= 3):
var = raw_input( \nPress\ \n1播放\n2暂停\n3退出\n)


如果var == 1:
打印正在播放...
playerengine_iface.Play()

elif var == 2:
打印暂停
playerengine_iface.Pause()

这是我尝试执行时得到的内容

 追踪(最近一次通话为最后):
< module>中的文件 dbus3.py,第4行。
bus_obj = dbus.bus.BusConnection( tcp:host = localhost,port = 12434)
文件 /usr/lib/python2.7/dist-packages/dbus/bus.py,第125行,在__new__
bus = cls._new_for_bus(address_or_type,mainloop = mainloop)
dbus.exceptions.DBusException:org.freedesktop.DBus.Error.NoServer:无法连接至套接字 localhost:12434 连接被拒绝

我在这里做错了什么?
我应该编辑/usr/lib/python2.7/dist-packages/dbus/bus.py



更新:



好,这是我添加

 < listen> tcp时的交易
: host = 192.168.1.7,port = 12434< / listen>

到/etc/dbus-1/session.conf,然后重新启动,希望它会开始监听重启后,
它永远不会启动。它被卡在加载屏幕上,有时,黑屏闪烁并显示以下文本:

 为每个用户会话配置的脉冲音频禁用;编辑/ etc / default / saned 

所以,当我按ctrl + alt + f1时,更改将session.conf恢复为原始状态并重新启动,即可正常启动。



这是怎么回事?
如何使dbus守护程序侦听tcp连接,而不会遇到问题?

解决方案

我最近需要对此进行设置并发现诀窍是: session.conf 中的< listen> 元素的顺序重要 code>。您应确保首先出现TCP元素。我知道奇怪,但至少在我看来,这是事实。 (如果我颠倒顺序,然后将UNIX套接字< listen> 放在首位,我会看到完全相同的黑屏行为。)



此外,在TCP < listen> 标记之前添加也是必要的,但还不够。要通过TCP进行远程D-Bus连接,您需要做三件事:


  1. 添加< listen> 标记在UNIX之上,类似于:

     < listen> tcp:host = localhost,bind = *,port = 55556,family = ipv4< / listen> 
    < listen> unix:tmpdir = / tmp< / listen>


  2. 添加一行(在< listen> 标记就可以了),

     < auth> ANONYMOUS< / auth> 


  3. 在这些内容下方添加另一行内容:

     < allow_anonymous /> 


<除了可能包含在会话中的任何其他< auth> 标记之外,还应添加auth> 标记。 conf 。总之,您的 session.conf 应该包含如下代码段:

 < listen> tcp:host = localhost,bind = *,port = 55556,family = ipv4< / listen> 
< listen> unix:tmpdir = / tmp< / listen>

< auth> ANONYMOUS< / auth>
< allow_anonymous />

完成这三件事之后,您应该能够远程连接到会话总线。在。直到我偶然发现他对这个问题,我不相信 dbus-send 支持远程



更新:如果您使用的是systemd(并希望访问系统总线),则可能还需要添加一个将 ListenStream = 55557 转换为 /lib/systemd/system/dbus.socket 的行,如下所示:

  [套接字] 
ListenStream = / var / run / dbus / system_bus_socket
ListenStream = 55557#<-添加这行

UPDATE2 :感谢@altagir指出最新版本的D-Bus 将启用 AppArmor 媒体可用的系统上,因此您可能还需要将< apparmor mode = disabled /> 添加到 session.conf / system.conf 获取这些说明。


I wrote a simple python program to play and pause banshee music player. While its working on my own machine, I have trouble doing it to a remote computer, connected to the same router (LAN). I edited the session.conf of the remote machine, to add this line:

<listen>tcp:host=localhost,port=12434</listen>

and here is my program:

    import dbus


    bus_obj=dbus.bus.BusConnection("tcp:host=localhost,port=12434")
    proxy_object=bus_obj.get_object('org.bansheeproject.Banshee',                              
    '/org/bansheeproject/Banshee/PlayerEngine')

    playerengine_iface=dbus.Interface(proxy_object,
    dbus_interface='org.bansheeproject.Banshee.PlayerEngine')

    var=0

    while (var!="3"):
        var=raw_input("\nPress\n1 to play\n2 to pause\n3 to exit\n")


            if var=="1":
                print "playing..."
                playerengine_iface.Play()

            elif var=="2":
                print "pausing"
                playerengine_iface.Pause()

This is what i get when i try to execute it

Traceback (most recent call last):
  File "dbus3.py", line 4, in <module>
    bus_obj=dbus.bus.BusConnection("tcp:host=localhost,port=12434")
  File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 125, in __new__
    bus = cls._new_for_bus(address_or_type, mainloop=mainloop)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NoServer: Failed to connect to socket "localhost:12434" Connection refused

What am I doing wrong here? should i edit /usr/lib/python2.7/dist-packages/dbus/bus.py

UPDATE:

ok, here is the deal when i add

<listen>tcp:host=192.168.1.7,port=12434</listen>

to to /etc/dbus-1/session.conf, then reboot, hoping it would start listening on reboot, It never boots. It gets stuck on loading screen and occasionally, a black screen with the following text flashes:

Pulseaudio Configured For Per-user Sessions Saned Disabled;edit/etc/default/saned

so, when i go ctrl+alt+f1 , change session.conf to original state and reboot, it boots properly.

Whats all that about? How can I make dbus daemon listen for tcp connections, without encountering problems?

解决方案

I recently needed to set this up, and discovered that the trick is: order matters for the <listen> elements in session.conf. You should make sure the TCP element occurs first. Bizarre, I know, but true, at least for my case. (I see exactly the same black screen behavior if I reverse the order and put the UNIX socket <listen> element first.)

Also, prepending the TCP <listen> tag is necessary, but not sufficient. To make remote D-Bus connections via TCP work, you need to do three things:

  1. Add a <listen> tag above the UNIX one, similar to this:

    <listen>tcp:host=localhost,bind=*,port=55556,family=ipv4</listen>
    <listen>unix:tmpdir=/tmp</listen>
    

  2. Add a line (right below the <listen> tags is fine) that says:

    <auth>ANONYMOUS</auth>
    

  3. Add another line below these that says:

    <allow_anonymous/>
    

The <auth> tag should be added in addition to any other <auth> tags that may be contained in your session.conf. In summary, your session.conf should contain a snippet that looks like this:

<listen>tcp:host=localhost,bind=*,port=55556,family=ipv4</listen>
<listen>unix:tmpdir=/tmp</listen>

<auth>ANONYMOUS</auth>
<allow_anonymous/>

After doing these three things, you should be able to connect to the session bus remotely. Here's how it looks when specifying a remote connection in D-Feet:

Note that, if you want to connect to the system bus, too, you need to make similar changes to /etc/dbus-1/system.conf, but specify a different TCP port, for example 55557. (Oddly enough, the element order appears not to matter in this case.)

The only weird behavior I've noticed in this configuration is that running Desktop apps with sudo (e.g., sudo gvim) tends to generate errors or fail outright saying "No D-BUS daemon running". But this is something I need to do so rarely that it hardly matters.

If you want to send to a remote machine using dbus-send, you need to set DBUS_SESSION_BUS_ADDRESS accordingly, e.g., to something like:

export DBUS_SESSION_BUS_ADDRESS=tcp:host=localhost,bind=*,port=55556,family=ipv4

This works even if the bus you want to send to is actually the system bus of the remote machine, as long as the setting matches the TCP <listen> tag in /etc/dbus-1/system.conf on the target. (Thanks to Martin Vidner for this tip. Until I stumbled across his answer to this question, I didn't believe dbus-send supported remote operation.)

UPDATE: If you're using systemd (and want to access the system bus), you might also need to add a line saying ListenStream=55557 to /lib/systemd/system/dbus.socket, like so:

[Socket]
ListenStream=/var/run/dbus/system_bus_socket
ListenStream=55557  # <-- Add this line

UPDATE2: Thanks to @altagir for pointing out that recent versions of D-Bus will enable AppArmor mediation on systems where it's available, so you may also need to add <apparmor mode="disabled"/> to session.conf/system.conf for these instructions to work.

这篇关于通过TCP连接到dbus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆