Note that there are some explanatory texts on larger screens.

plurals
  1. PORunning Python file from Django views
    text
    copied!<p>I understand this question has been asked before but the methods adviced don't work for me. This is what I want to do from my Django view:</p> <pre><code>sudo python mypythonscript.py arg1 arg2 &gt; mypythonscript.log </code></pre> <p>If I execute this from command line it works like a charm but can't get it to work through django views. I have tried using os.system(command) and subprocess.call(command, shell=True) but they don't work. Thanks in advance.</p> <p>EDIT: This is my views.py:</p> <pre><code>from django.http import HttpResponse import datetime import subprocess def li_view(request): if request.is_ajax(): if request.method == 'GET': message = "This is an XHR GET request" elif request.method == 'POST': message = "This is an XHR POST request" print request.POST else: message = "No XHR" num_instances = request.POST['num_instances'] ami_id = "ami-ff02058b" command = "sudo python /home/bitnami/launch_instances.py 1 " + num_instances + " " + ami_id + " &gt; /home/bitnami/launcinstances.log" subprocess.call(commad, shell=True) return HttpResponse("something creative will go here later") </code></pre> <p>The whole story is that I have a form on my website and I want to pass the contents of that form as arguements to my launch_instances.py script. When I press the submit button in my form it posts to /luanch_instances/ which 'redirects' to this view. Executing the code as it is will do nothing, it'll simply just show me "something creating will go here later" on a new page. If I was to how ever use</p> <pre><code>suprocess.check_call(command, shell=True) </code></pre> <p>This is what I get:</p> <pre><code>Command 'sudo python /home/bitnami/launch_instances.py 1 ami-ff02058b &gt; /home/bitnami/launchinstances.log' returned non-zero exit status 2 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload