在 Django 中处理 subprocess.call() [英] Processing subprocess.call() in Django
问题描述
我正在开发的应用程序的简单想法是用户给出 Linux 命令,Linux 命令的结果将显示在网络浏览器中.这是我的 views.py:
The simple idea of the app I am developing is user give the Linux commands and the result of Linux command will be shown in the webbrowser. Here's my views.py:
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
import subprocess
globalcmd = 0
globalresult = 0
def ls(request):
if request.method == 'POST':
globalcmd = request.POST.get('command', False)
globalresult = subprocess.call(['globalcmd'], shell=True)
return HttpResponseRedirect('/thanks/')
else:
pass
return render_to_response('form.html', {'cmd':'cmd'}, context_instance=RequestContext(request))
def show_template(request):
return render_to_response('thanks.html', {'globalok':globalresult}, context_instance=RequestContext(request))
我从 form.html 获取输入,该输入由视图 'ls' 处理.作为用户,我只是使用 ls 命令进行测试.每当我按下 ls 命令时,它都会由 suprocess.call 处理并存储在 globalresult 中,然后在Thanks.html 中调用它.我的输出是 0.我做错了什么?这是Thanks.html:
I get input from form.html which is processed by view 'ls'. As a user I am just testing with ls command. Whenever I press ls command it is processed by suprocess.call and stored in globalresult which is later called in thanks.html. My output is 0. What am I doing wrong? Here's thanks.html:
<h1>
{{ globalresult }}
</h1>
推荐答案
查看 您正在调用的函数,结果是调用的返回码,而不是命令本身的输出.所以,我认为你的代码完全按照它应该做的.
Check the documentation of the function you are calling, the result is the return-code of the invocation, not the output of the command itself. So, I think that your code does exactly what it should.
也许您打算调用 subprocess.check_output?
作为旁注,要非常小心这种网络终端交互;如果您在没有适当安全性的情况下将此 Web 应用程序暴露给 Internet,将会发生坏事....但您可能知道这一点.
As a side note, be very careful with this web-terminal interaction; if you expose this web application to the internet without proper security bad things will happen.... but you probably know that.
这篇关于在 Django 中处理 subprocess.call()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!