我想通过Python中的脚本来控制正在运行的进程/程序。我有一个程序“ linphonec”(您可以安装:apt-get install linphonec)。我的任务是:
linphonec
(此刻我正在使用子流程)linphonec
运行时,它有许多命令来控制它,我想例如使用proxy list
(中的命令linphonec
)。简单流程:
test@ubuntu$ > linphonec
linphonec > proxy list
我怎样才能做到这一点?
实际上有两种通信方式:
运行您的程序myprogram.py | linphonec
以将所有内容传递print
给linphonec
使用subprocess.Popen与subprocess.PIPE经由keywrod参数构造为标准输入(propably stdout和stderr,太),然后进行通信为单个命令或使用标准输入和标准输出(stderr的)作为文件
import subprocess
p=subprocess.Popen("linphonec",
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True) #this is for text communication
p.stdin.write("proxy list\n")
result_first_line=p.stdout.readline()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句