使用stdin的Python子进程

时间:2019-06-10 23:38:48

标签: python subprocess communicate

我正在尝试使用python子进程来调用第三方命令ParseAddress。命令ParseAddress打印出一行文本,要求用户输入地址列表,然后根据输入内容打印出堆栈跟踪信息。如果我手动运行该命令,则它看起来像:

$ParseAddress Appname                       <- Run the command in console
Paste the addresses - new line to continue
[0]0xaaaaaaaa                               <- a User paste a list of address here
[1]0xbbbbbbbb
[2]0xcccccccc
                                            <- the user type 'enter' to continue
Stack Trace info:                           <-- results of running the ParseAddress
0xaaaaaaaa: Test::info::Runtest
0xbbbbbbbb: Test::info::SingleTestCase
0xcccccccc: ...

因此,如果我已经具有文本格式的列表地址,那么我认为subprocess()和communication()在这种情况下会有所帮助:

cmd = subprocess.Popen([ParseAddress, Appname], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
output, error = cmd.communicate(input="[0]0xaaaaaaaa\n[1]0xbbbbbbbb\n[2]0xcccccccc\n\n")
print(output)

当我将子进程与communite()一起使用时,控制台输出为:

$ParseAddress Appname
Paste the addresses - new line to continue
0xaaaaaaaa: ?? ??:0                         <- subprocess.PIPE?
0xbbbbbbbb: ?? ??:0
0xcccccccc: ?? ??:0

Stack Trace info: <-- no results from the ParseAddress since the address info are in incorrect format

如果我使用

cmd.communicate(input="[0]0xaaaaaaaa\r\n[1]0xbbbbbbbb\r\n[2]0xcccccccc\r\n\r\n")

结果是:

$ParseAddress Appname
Paste the addresses - new line to continue
                                           <-- No address here, no ideas why
Stack Trace info:

我的问题是为什么参数从[0]0xaaaaaaaa更改为0xaaaaaaaa: ?? ??:0

使用communication()的正确方法是什么?

环境:
Ubuntu 18.04
Python 2.7.16 :: Anaconda,Inc.

0 个答案:

没有答案