def ExecCmd(self, strCommand, bReqRet=True,timeout=10.0):
LogDebug("apsExecCmd: Start")
LogDebug("Command: ", strCommand)
# st = subprocess.STARTUPINFO
# st.dwFlags = subprocess.STARTF_USESHOWWINDOW
# st.wShowWindow = subprocess.SW_HIDE
try:
if bReqRet:
outfilename = os.path.join(_apsGetProjectPath(), "Apus/ApsBase/_log/cmdout{}.log".format(getCurTimeStr()))
errfilename = os.path.join(_apsGetProjectPath(), "Apus/ApsBase/_log/cmderr{}.log".format(getCurTimeStr()))
with io.open(outfilename, 'wb') as outwriter, io.open(outfilename, 'rb', 1) as outreader,\
io.open(errfilename, 'wb') as errwriter, io.open(errfilename, 'rb', 1) as errreader:
# process = subprocess.Popen(strCommand, stdout=outwriter, stderr=errwriter, startupinfo=st)
process = subprocess.Popen(strCommand, stdout=outwriter, stderr=errwriter, shell=True)
expireTime= timeout+time.time()
while process.poll() is None and time.time() < expireTime:
time.sleep(0.5)
if time.time() > expireTime:
process.kill()
strResult = outreader.read()
strErrInfo = errreader.read()
if strErrInfo == '':
LogDebug("out info:", strResult)
return APUS_SUCCESS, strResult
else:
LogError("err info:", strErrInfo)
return APUS_FAIL, strErrInfo
else:
# process = subprocess.Popen(strCommand,, shell=True startupinfo=st)
process = subprocess.Popen(strCommand, shell=True)
LogDebug("Command Sent")
return APUS_SUCCESS, process
except:
LogError("Exception Info:", sys.exc_info()[1])
raise sys.exc_info()[1]
python 调用cmd命令通过文件来实现
最新推荐文章于 2024-04-26 13:54:31 发布