Use Popen to execute a Powershell script in Python, how can I get the Powershell script's output and update it to web page?
I create a simple HTML with a button. When user clicks the button, it will call a Python file executed in server side. In the Python file, I use Popen to call a Powershell script, like below code:
command_line = r'"C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe" -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -c ". \"C:\DoSth\DoSth.ps1\""'
args = shlex.split(command_line)
sys.stdout.flush()
retcode = subprocess.Popen(args)
But the Powershell requires a long time to finish(and generate lots of output during execution). After user clicks the button, the browser shows "Waiting for xxx" about several minutes until the python code executed completely.
The question is: how ca开发者_StackOverflown I get the Powershell's output during the execution, and update the output to browser in time?
Is it perhaps possible to use the stdout
argument to subprocess.Popen
?
I mean something like:
retcode = subprocess.Popen(args, stdout=sys.stdout)
精彩评论