1

I basically have a backend program written in perl. This program pushes some configuration onto a network device. It gives a lot of output and I want users to be able to see the program as it runs. Up to now this has been easy as I have been running from the terminal.

Now I am trying to write a django app. I basically want a user to press submit, the browser to bring them to a new page and on this new page I want the user to see the text output of that program as it runs.

I have managed to get the program running in the backgroound using: http://docs.python.org/library/subprocess.html

Say the below is a simple request. In the response webpage I want to see the output of the program running live or at least refresh to see the latest output every few seconds (might be a workaroudn for now)

def config(request):
    builder_Config_list = Config.objects.all().order_by('-generated_date')[:100]
    if 'hostname' in request.POST and request.POST['hostname']:
        hostname = request.POST['hostname']
        command = "path/to/builder.pl --router " + hostname
        result = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
        return render_to_response('config/config.html', {'result':result, } )
evolution
  • 4,332
  • 5
  • 29
  • 34
  • This seems very reasonable - what's your question? – jMyles Jul 06 '11 at 16:52
  • Sorry I should have added a final question. It is "How would I adjust the above snippet of code in such a way the the program "builder.pl" gives output to screen. By the output.pl is basically an expect script. It logs into a router and puts on some config. I want the user to see this happen on browser screen. Thanks – evolution Jul 06 '11 at 17:05

1 Answers1

2

You can read the output of the subprocess like so...

pipe = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
result = pipe.stdout.read() # this is the output of the process
return render_to_response('config/config.html', {'result': result})

However, you will only be able to see the result after the process has finished. If you want to see the output as the program runs, that will be a bit tougher. I suppose it could be accomplished by putting the subprocess call into a separate process or thread, having the process write to a file or message queue, and then reading from that file in your view.

Zhehao Mao
  • 1,789
  • 13
  • 13
  • Yea, having the process output its STDERR and STDOUT to files, and then displaying those files separately is probably the best solution. Of course, added complexity is needed to store the file names and the users they are associated with in order to restore lost sessions, etc. – kcbanner Jul 06 '11 at 17:50
  • Ya I think stdout and stderr can be put in a file handy enough. The above code looks like it will either block until the code is output or partially give a response so it will only show start of the file. This said I need to try to make sure :-) I guess I could output to file and refresh the screen every few minutes showing that output by setting some HTTP META tag to set the screen to refresh but it seems a bit old fashioned. – evolution Jul 06 '11 at 17:56
  • could i possibly do this using something like: http://stackoverflow.com/questions/2922874/how-to-stream-an-httpresponse-with-django - Although i tried this and so far it just gives me a static page with first output from the generator function. – evolution Jul 07 '11 at 00:58
  • Oh acutally it worked!!! I was just missing the time module. Should have looked at the server debugs. – evolution Jul 07 '11 at 01:07