是否可以使用subprocess.Popen与子子进程通信?

时间:2022-11-03 15:41:35

I'm trying to write a python script that packages our software. This script needs to build our product, and package it. Currently we have other scripts that do each piece individually which include csh, and perl scripts. One such script is run like:

我正在尝试编写一个包装我们软件的python脚本。这个脚本需要构建我们的产品并打包它。目前我们有其他脚本分别执行每个部分,包括csh和perl脚本。一个这样的脚本运行如下:

sudo mod args

where mod is a perl script; so in python I would do

其中mod是perl脚本;所以在python我会这样做

proc = Popen(['sudo', 'mod', '-p', '-c', 'noresource', '-u', 'dtt', '-Q'], stderr=PIPE, stdout=PIPE, stdin=PIPE)

The problem is that this mod script needs a few questions answered. For this I thought that the traditional

问题是这个mod脚本需要回答几个问题。为此,我认为是传统的

(stdout, stderr) = proc.communicate(input='y')

would work. I don't think it's working because the process that Popen is controlling is sudo, not the mod script that is asking the question. Is there any way to communicate with the mod script and still run it through sudo?

会工作。我不认为这是有效的,因为Popen控制的过程是sudo,而不是提出问题的mod脚本。有没有办法与mod脚本进行通信并仍然通过sudo运行它?

4 个解决方案

#1


4  

I would choose to go with Pexpect.

我会选择和Pexpect一起去。

import pexpect
child = pexpect.spawn ('sudo mod -p -c noresource -u dtt -Q')
child.expect ('First question:')
child.sendline ('Y')
child.expect ('Second question:')
child.sendline ('Yup')

#2


4  

I think you should remove the sudo in your Popen call and require the user of your script to type sudo.

我认为你应该删除你的Popen调用中的sudo并要求你的脚本用户键入sudo。

This additionally makes more explicit the need for elevated privileges in your script, instead of hiding it inside Popen.

这另外使得在脚本中需要提升权限,而不是将其隐藏在Popen中。

#3


1  

The simplest thing to do would be the run the controlling script (the Python script) via sudo. Are you able to do that, or is that not an option?

最简单的方法是通过sudo运行控制脚本(Python脚本)。你能做到这一点,还是不是一个选择?

#4


0  

We need more information.

我们需要更多信息。

  1. Is sudo asking you for a password?
  2. sudo是否要求您输入密码?

  3. What kind of interface does the mod script have for asking questions?
  4. mod脚本提出什么样的界面来提问?

Because these kind of things are not handled as normal over the pipe.

因为这些东西在管道上不能正常处理。

A solution for both of these might be Pexpect, which is rather expert at handling funny scripts that ask for passwords, and various other input issues.

这两者的解决方案可能是Pexpect,它在处理要求密码的有趣脚本以及各种其他输入问题方面相当专业。

#1


4  

I would choose to go with Pexpect.

我会选择和Pexpect一起去。

import pexpect
child = pexpect.spawn ('sudo mod -p -c noresource -u dtt -Q')
child.expect ('First question:')
child.sendline ('Y')
child.expect ('Second question:')
child.sendline ('Yup')

#2


4  

I think you should remove the sudo in your Popen call and require the user of your script to type sudo.

我认为你应该删除你的Popen调用中的sudo并要求你的脚本用户键入sudo。

This additionally makes more explicit the need for elevated privileges in your script, instead of hiding it inside Popen.

这另外使得在脚本中需要提升权限,而不是将其隐藏在Popen中。

#3


1  

The simplest thing to do would be the run the controlling script (the Python script) via sudo. Are you able to do that, or is that not an option?

最简单的方法是通过sudo运行控制脚本(Python脚本)。你能做到这一点,还是不是一个选择?

#4


0  

We need more information.

我们需要更多信息。

  1. Is sudo asking you for a password?
  2. sudo是否要求您输入密码?

  3. What kind of interface does the mod script have for asking questions?
  4. mod脚本提出什么样的界面来提问?

Because these kind of things are not handled as normal over the pipe.

因为这些东西在管道上不能正常处理。

A solution for both of these might be Pexpect, which is rather expert at handling funny scripts that ask for passwords, and various other input issues.

这两者的解决方案可能是Pexpect,它在处理要求密码的有趣脚本以及各种其他输入问题方面相当专业。