def run_command(self, cmd):
try:
import subprocess
except ImportError:
# Python 2.3
_, rf, ef = os.popen3(cmd)
else:
# Python 2.4+
p = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
rf, ef = p.stdout, p.stderr
errors = ef.read()
if errors:
print("ERROR: %s" % errors)
return rf.read().strip()
该函数返回shell命令的执行结果,使用前需要先执行import os