首页 > 解决方案 > Read output from subprocess cli, yowsup-cli to store the output

问题描述

I have made the following subprocess to interact with the yowsup-cli.

connection_string = "python /root/yowsup/yowsup-cli demos --yowsup --config config.json"
popen_parameters = connection_string.split(" ")
proc = Popen(popen_parameters, stdout=PIPE, stderr=PIPE)

out, err = proc.communicate()

The interaction works fine I am able to send parameters but I have no return from the CLI of the yowsup-cli. the return is working in the background.
I need to send some variables from the input and receive the result from yowsup-cli.

标签: pythonsubprocessyowsup

解决方案


我使用 Pexpect 并创建了一个算法来实现我的目标。对于那些需要创建自动化的人来说,这是我使用的:

child = pexpect.spawn('/bin/bash')
fout = open('mylog.txt','wb')
child.logfile = fout

child.sendline('/usr/bin/python /root/yowsup/yowsup-cli demos --yowsup --config config.json')
child.expect('offline') 

bash诀窍是在运行 python 脚本之前创建一个子进程。

在最后一行之后,child.expect('offline')您可以按照适合您的海豚的逻辑发送所需的命令。

希望这对其他人有帮助


推荐阅读