我正在运行一个适用于套接字的脚本..它需要sudo才能运行.. 然而, 在脚本内部,我调用另一个不需要以sudo身份运行的脚本
这是代码:
import subprocess
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.settimeout(5.0)
host='192.168.1.148'
port=1022
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
subprocess.call("python bluetooth2.py",shell=True)
print 'got connection from',addr
c.send('Thank you for connecting')
#c.settimeout(5.0)
c.recv(1022)
c.close()
bluetooth2.py
运行pulseaudio,由于某种原因以root身份运行并且不起作用。任何帮助非常感谢!
以下是bluetooth2.py脚本的参考信息(调用pulseaudio的那个)
import time
import pexpect
from sh import bluetoothctl
import subprocess
mac = "C8:84:47:26:E6:3C"
print ("stuck here")
#bluetoothctl("connect", mac)
def connect():
child = pexpect.spawn('bluetoothctl')
child.sendline('power on')
child.sendline('agent on')
child.sendline('default-agent')
child.sendline('pair C8:84:47:26:E6:3C')
time.sleep(1)
child.sendline('trust C8:84:47:26:E6:3C')
time.sleep(1)
child.sendline('connect C8:84:47:26:E6:3C')
print("connecting...")
time.sleep(5)
subprocess.call("pulseaudio --start",shell=True)
subprocess.call("pacmd set-default-sink
bluez_sink.C8_84_47_26_E6_3C",shell=True)
subprocess.call("aplay /home/pi/bleep_01.wav", shell=True)
答案 0 :(得分:1)
解决方案为所有用户运行 PulseAudio
将波纹管添加到/etc/systemd/system/pulseaudio.service文件并保存
[Unit]
Description=PulseAudio system server
[Service]
Type=notify
ExecStart=pulseaudio --daemonize=no --system --realtime --log-target=journal
[Install]
WantedBy=multi-user.target
Enable service
sudo systemctl --system enable pulseaudio.service
sudo systemctl --system start pulseaudio.service
sudo systemctl --system status pulseaudio.service
Edit Client conf /etc/pulse/client.conf 并替换下面的屁股
default-server = /var/run/pulse/native
autospawn = no
将根添加到脉冲组
sudo adduser root pulse-access
最后重启系统