2

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

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>

2 Answers 2

4

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.

Maybe you intended to call subprocess.check_output?

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Where do I get the output of the linux command in web then? res = os.system('rsync --list-only /home') return render_to_response('thanks.html', {'res':res}, context_instance=RequestContext(request)) also doesn't work
@user1755133: If you've missed it somehow: to get output of a command as a string you could: subprocess.check_output(['/path/to/command', 'arg1', 'arg2', '..'])
I got it. But the problem is I am using Django in Debian with Python version 2.6.6. Thus, there's no check_output in python 2.6.6
Indeed, there's a way to do so in Python 2.6.6 too. subprocess.Popen(['ls', '/home/'], stdout=subprocess.PIPE).communicate()[0]
1

you're not passing globalresult to the thanks.html template in show_template()

you probably want

return render_to_response('thanks.html', {'globalresult':globalresult}, context_instance=RequestContext(request))

if lazy you can also do

return render_to_response('thanks.html', locals(), context_instance=RequestContext(request))

...though it seems like you're trying to do a redirect to get to the thank you page (?). In that case, it would be better to just render the thank you view immediatley

e.g. replace this line

return HttpResponseRedirect('/thanks/')

with either of the two lines above ^ in ma answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.