如何使用 subprocess.popen
How subprocess.popen is used
为什么我在 运行 subprocess.popen 时出现错误?
我加载了一个带有变量参数的命令,我进行了拆分并将结果传递给 subprocess.Popen。 它returns一个错误。
非常感谢大家的配合
完整代码如下:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import subprocess
import shlex
def main():
try:
data = "ls -al"
args = shlex.split(data)
comando = subprocess.Popen(args, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
if comando.stderr.read() != "":
print("[-] Error")
else:
print(comando.stdout.read())
except Exception as e:
print (e)
if __name__ == '__main__' :
try:
main()
except KeyboardInterrupt:
exit()
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import subprocess
import shlex
def main():
try:
data = "ls -al"
args = shlex.split(data)
comando = subprocess.Popen(args, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
comando_stdout, comando_stderr = comando.communicate()
if comando_stderr:
print("[-] Error")
else:
print(comando_stdout)
except Exception as e:
print (e)
if __name__ == '__main__' :
try:
main()
except KeyboardInterrupt:
exit()
为什么我在 运行 subprocess.popen 时出现错误?
我加载了一个带有变量参数的命令,我进行了拆分并将结果传递给 subprocess.Popen。 它returns一个错误。
非常感谢大家的配合
完整代码如下:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import subprocess
import shlex
def main():
try:
data = "ls -al"
args = shlex.split(data)
comando = subprocess.Popen(args, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
if comando.stderr.read() != "":
print("[-] Error")
else:
print(comando.stdout.read())
except Exception as e:
print (e)
if __name__ == '__main__' :
try:
main()
except KeyboardInterrupt:
exit()
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import subprocess
import shlex
def main():
try:
data = "ls -al"
args = shlex.split(data)
comando = subprocess.Popen(args, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
comando_stdout, comando_stderr = comando.communicate()
if comando_stderr:
print("[-] Error")
else:
print(comando_stdout)
except Exception as e:
print (e)
if __name__ == '__main__' :
try:
main()
except KeyboardInterrupt:
exit()