3

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 can I get the Powershell's output during the execution, and update the output to browser in time?

Landy
  • 379
  • 1
  • 4
  • 10
  • So you want to update live? By live I mean, you want to update as soon as `subprocess` returns – user225312 Jan 12 '11 at 13:32
  • @sukhbir: yes, I want to update live. The Powershell script will print lots of output like "connected to server1\n", "connected to server2\n", I hope these information can be updated in browser side in time: when Powershell print "connected to server1\n", the browser also display "connected to server1\n"; then Powershell print ""connected to server2\n", and browser also display "connected to server2\n"... – Landy Jan 12 '11 at 15:04

1 Answers1

0

Is it perhaps possible to use the stdout argument to subprocess.Popen?

I mean something like:

retcode = subprocess.Popen(args, stdout=sys.stdout)
Jasmijn
  • 9,370
  • 2
  • 29
  • 43
  • It is. See Python documentation of [subprocess](http://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline). – webwurst May 10 '13 at 13:28