subprocess.run 导致 space 解释为 arg?
subprocess.run leads to space interpreted as arg?
我正在编写一个非常简单的测试套件,当我调用 subprocess.run(["./name-of-binary", args], capture_output=True, shell=True)
时,它会存储命令的输出。当我用 args = ""
调用它时,它似乎将 space 传递给二进制文件。当我 运行 带有实际 space 的二进制文件时,一切正常。这是怎么回事?谢谢!
可能的解决方案(推荐):
if args:
subprocess.run(["./name-of-binary"], capture_output=True)
else:
subprocess.run(["./name-of-binary", args], capture_output=True)
或者:
subprocess.run("".join(["./name-of-binary", args]), capture_output=True, shell=True) # this maybe will cause shell injection.
我认为原因是当执行这个命令时,subprocess
会在element.So之间自动添加space,直接的方法是直接传递字符串。
我正在编写一个非常简单的测试套件,当我调用 subprocess.run(["./name-of-binary", args], capture_output=True, shell=True)
时,它会存储命令的输出。当我用 args = ""
调用它时,它似乎将 space 传递给二进制文件。当我 运行 带有实际 space 的二进制文件时,一切正常。这是怎么回事?谢谢!
可能的解决方案(推荐):
if args:
subprocess.run(["./name-of-binary"], capture_output=True)
else:
subprocess.run(["./name-of-binary", args], capture_output=True)
或者:
subprocess.run("".join(["./name-of-binary", args]), capture_output=True, shell=True) # this maybe will cause shell injection.
我认为原因是当执行这个命令时,subprocess
会在element.So之间自动添加space,直接的方法是直接传递字符串。