django前端运行subprocess输出到浏览器窗口 [英] django frontend to run subprocess output to browser window

查看:1157
本文介绍了django前端运行subprocess输出到浏览器窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上有一个用Perl编写的后端程序。该程序将一些配置推送到网络设备上。它提供了大量的输出,我希望用户能够在程序运行时看到程序。到目前为止,这是很容易的,因为我已经从终端运行。



现在我试图写一个django应用程序。我基本上希望用户按提交,浏览器将它们带到一个新页面,在这个新页面上,我希望用户在运行该程序时看到文本输出。



我设法使用以下方式使程序运行:
http: //docs.python.org/library/subprocess.html



说下面是一个简单的请求。在响应网页中,我想看到程序的输出正在运行或至少刷新,以便每隔几秒看一次最新的输出(现在可能是一个workaroudn)

  def config(request):
builder_Config_list = Config.objects.all()。order_by(' - generated_date')[:100]
如果请求中有主机名 .POST和request.POST ['hostname']:
hostname = request.POST ['hostname']
command =path / to / builder.pl --router+主机名
result = subprocess.Popen(command.split(),stdout = subprocess.PIPE)
return render_to_response('config / config.html',{'result':result,})


解决方案

您可以像这样读取子进程的输出...

  pipe = subprocess.Popen(command.split(),stdout = subprocess.PIPE)
result = pipe.stdout.read()这是进程的输出
返回render_to_response('config / c onfig.html',{'result':result})

但是,您只能过程结束后查看结果。如果要在程序运行时看到输出,那将会更加困难。我想这可以通过将子进程调用放入一个单独的进程或线程中,将进程写入文件或消息队列,然后从视图中的该文件中读取。


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, } )

解决方案

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.

这篇关于django前端运行subprocess输出到浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆