简单的问题:
为什么这不起作用?
test = 2
print subprocess.check_output(["program",
"-v",
"-a5",
"-t%i", <----------
"-oURL",
"-uUSER",
"-pPASS"]) % (test)
Run Code Online (Sandbox Code Playgroud)
使用“-t1”或其他值,它可以完美工作。
我真的遇到了一个问题,我希望有人可以帮助我。我正在尝试在 Python3.1 中为名为 的命令行程序创建一个包装器spooky。我可以在命令行上成功运行该程序,如下所示:
$ spooky -a 4 -b .97
Run Code Online (Sandbox Code Playgroud)
我对 spooky 的第一次 Python 包装尝试如下所示:
import subprocess
start = "4"
end = ".97"
spooky_path = '/Users/path/to/spooky'
cmd = [spooky_path, '-a', start, '-b', end]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
process.wait()
print('Done')
Run Code Online (Sandbox Code Playgroud)
上面的代码打印Done,但不执行程序spooky
接下来我尝试在命令行上执行该程序,如下所示:
$ /Users/path/to/spooky -a 4 -b .97
Run Code Online (Sandbox Code Playgroud)
上面的代码也失败了,并且没有提供任何有用的错误。
我的问题是:如何通过发送spooky -a 4 -b .97到命令行让Python运行这个程序?我非常感谢您能提供的任何帮助。提前致谢。
对于 Linux 系统,我正在用 Python 编写一个程序,它会生成子进程。我正在使用“多处理”库,我想知道是否有一种方法可以使用与当前用户不同的用户调用子流程。我希望能够使用不同的用户运行每个子进程(例如 Postfix)。
有什么想法或指示吗?
当命令将用户输入作为其某些参数时,我使用 fork()/exec()/wait() 而不是 system(),因此用户不能输入类似...
&rm -rf /home/* && echo HAHA
Run Code Online (Sandbox Code Playgroud)
...作为一个论点。
我假设 popen 与 system() 一样危险,因为它需要单个字符串,而不是像 exec 系列函数那样需要字符串列表。
不过,我只能从 exec 函数获取返回值。是否有一个“安全”版本的 popen,我可以使用用户输入运行并在父进程中处理 stdout / stderr ?
我有一个 Python 脚本,需要发出许多 shell 命令。我认为我可以创建一个子进程对象,然后在每次执行命令时重用它。
这就是我设置代码的方式:
def setupPipeline(self):
setupShell = subprocess.Popen([''], stdout=subprocess.PIPE, shell=True, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
# Stop running pipeline
setupShell.stdin.write('stop ' + self.Name)
output = setupShell.stdout.read()
print output
# Cancel any running jobs and cleanup variables
setupShell.stdin.write('sudo -u pr cancel ALL')
output = setupShell.stdout.read()
print output
setupShell.stdin.write('sudo -u pr clean ALL')
output = setupShell.stdout.read()
print output
Run Code Online (Sandbox Code Playgroud)
(这里跳过很多其他代码)
if __name__ == '__main__':
#self-test code
pipelineObj = Pipeline(sys.argv)
pipelineObj.setupPipeline()
Run Code Online (Sandbox Code Playgroud)
但是,当代码到达第二个命令时,我得到一个
IOError: [Errno 32] Broken pipe
Run Code Online (Sandbox Code Playgroud)
如何重用子进程对象来发出需要在同一 shell 中执行的命令?这些命令不能简单地链接在一起,因为每次调用之间都会进行处理。
在 python 中我打开了 4 个子进程。现在,当 python 脚本中出现新请求时,我想终止所有先前的进程。
我正在使用 python 2.7 和 windows 7 操作系统。
谢谢,
我正在使用带有许多参数的 Subprocess Popen 执行第 3 方应用程序
myprocess=Popen(['executionlist','with','arguments'],stdout=PIPE,stderr=PIPE)
myprocess.communicate()
Run Code Online (Sandbox Code Playgroud)
获取我的 stdout 和 err 元组,第 3 方应用程序在后台启动,因此我在 stdout 中获取其 pid...[如果我尝试在前台运行,它会抛出“打开终端时出错:未知”] 我正在使用 psutil跟踪所有这些分叉并使用以下方式监控它:
EXITCODE=psutil_obj.wait(timeout=xxx)
Run Code Online (Sandbox Code Playgroud)
应用程序发送不同的退出代码,我需要在 EXITCODE 中访问这些代码,但当我在不同的 python 脚本中运行它时,它总是给我“无”值......
根据 https://code.google.com/p/psutil/wiki/Documentation “Wait() 进程终止,如果该进程是当前进程的子进程,也返回退出代码,否则无。”
无论如何,我是否可以从独立进程 ID 访问退出代码,而不是由 Popen 专门分叉的?
我在尝试将简单的 grep 命令输入 python 时遇到问题。我想在文件或列表中获取以下命令的输出。
grep -c 'some thing' /home/user/* | grep -v :0
Run Code Online (Sandbox Code Playgroud)
这就是我所拥有的,但它根本不起作用......
thing = str(subprocess.Popen(['grep', '-c', 'some thing', '/home/user/*', '|', 'grep', '-v', ':0'], stdout=subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)
基本上,我需要搜索目录中的文件,如果目录中的任何文件中缺少我的字符串,则返回结果。
工作代码(谢谢!!):
thing = subprocess.Popen(('grep -c "some thing" /home/user/* | grep -v ":0"' ),shell=True, stdout=subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 python 子进程 ping 服务器,但出现以下错误 -
>>> import subprocess as sp
>>> sp.getstatusoutput('ping 127.0.0.1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getstatusoutput'
Run Code Online (Sandbox Code Playgroud) 我定义了这个类:
class InternalProc:
@staticmethod
def get_data():
try:
result = subprocess.run(['bridge-client', '--business-credentials'],
stdout=subprocess.PIPE)
data = json.loads(result.stdout.decode('utf-8'))
return data
except Exception:
logger.error("Unable to fetch the data")
raise
Run Code Online (Sandbox Code Playgroud)
我想对 get_data() 进行单元测试,我应该如何模拟 subprocess.run?我还想断言是否引发了异常?
subprocess ×10
python ×8
windows ×2
c ×1
exit-code ×1
fork ×1
mocking ×1
pipe ×1
popen ×1
process ×1
pytest ×1
pytest-mock ×1
python-2.7 ×1
python-3.x ×1
shell ×1
wrapper ×1