我明白這個問題之前已經被問過,但建議的方法不適用於我。 這是我想從我的Django的觀點做:從Django的意見中運行Python文件
sudo python mypythonscript.py arg1 arg2 > mypythonscript.log
如果我執行這個命令行它就像一個魅力,但不能讓它通過Django的視圖的工作。 我試過使用os.system(command)和subprocess.call(command,shell = True),但它們不起作用。 在此先感謝。
編輯: 這是我的views.py:
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 + " > /home/bitnami/launcinstances.log"
subprocess.call(commad, shell=True)
return HttpResponse("something creative will go here later")
整個故事是,我有我的網站上填寫表單,我想這種形式的內容arguements傳遞給我的launch_instances.py腳本。 當我按下我的表單中的提交按鈕時,它會發送到/ luanch_instances /,它將'重定向到'該視圖。 按照原樣執行代碼將不會執行任何操作,它只會在新頁面上向我顯示「稍後將創建一些內容」。 如果我是怎麼過使用
suprocess.check_call(command, shell=True)
這就是我得到:
Command 'sudo python /home/bitnami/launch_instances.py 1 ami-ff02058b > /home/bitnami/launchinstances.log' returned non-zero exit status 2
您需要告訴我們爲什麼它沒有工作。顯示你的代碼和輸出。 – 2012-08-01 08:26:06
另外你想通過在python視圖中做到這一點來完成什麼?通常最好使用諸如Celery這樣的任務隊列來執行這種事情,因爲它不會捆綁你的Web服務器。 – 2012-08-01 08:32:51
@VladimirVolodin我已經添加了輸出和代碼 – AmirHBP 2012-08-01 08:48:17