【发布时间】:2019-03-23 09:11:03
【问题描述】:
我正在编写的应用程序通过 HTTP 从网络检索 shell 脚本,我想在 python 中运行这个脚本但是我不想将它物理保存到硬盘驱动器,因为我的内存中已经有它的内容,并且我只想执行它。我尝试过这样的事情:
import subprocess
script = retrieve_script()
popen = subprocess.Popen(scrpit, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdOut, stdErr = popen.communicate()
def retrieve_script_content():
# in reality I retrieve a shell script content from network,
# but for testing purposes I will just hardcode some test content here
return "echo command1" + "\n" + "echo command2" + " \n" + "echo command3"
这个 sn-p 不起作用,因为 subprocess.Popen 期望您一次只提供一个命令。
有没有其他方法可以从内存中运行 shell 脚本?
【问题讨论】:
标签: python shell subprocess sh popen