我正在尝试编写一个启动子进程并写入子进程标准输入的 python 脚本。对输出进行一些测试,然后将更多命令写入标准输入。
我试过了:
def get_band():
print "band"
p = subprocess.Popen(["/path/to/program","-c","-"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
ran_stdout = p.communicate(input='show status')[0]
print(ran_stdout)
但是打印语句给出:
Unable to connect at 127.0.0.1, Connection refused.
我想知道我这样做是否正确?这是有关我尝试运行的过程的文档。我想使用最后一个选项。
Running the tool from the Linux shell allows additional options, depending on the options given to the command. The options are as follows:
-h Displays help about the command
-c <Filename> Instead of taking typed commands interactively from a user the commands are read from the named file, i.e. in batch mode. When all commands are processed the CLI session ends automatically.
-c - As above but reads command from Linux stdin. This allows commands to be ‘piped’ to the program.