【问题标题】:How can I start a python subprocess command on linux shell from within Django web app?如何从 Django Web 应用程序中在 linux shell 上启动 python 子进程命令?
【发布时间】:2010-09-20 09:36:31
【问题描述】:

我有一个网络应用程序,它读取并显示由 python 测试文件生成的日志文件。目前我可以从 cli 启动 python 文件,它工作正常。

但是我希望能够从应用程序中启动 python 测试。

此时我有一个“启动测试”按钮,它调用 views.py 文件中的函数

def launch_tests(request, test_txt_file):
    test_list_file = test_txt_file
    launcher2.main(test_list_file)

    return render_to_response('front_page.html')

文本文件包含要执行的 .py 文件的名称

在launcher2.main中

import os,time, string, pdb, sys
import getopt,subprocess
import pdb

bi_python_home = '//belfiler1/scratch-disk/conor.doherty/dev/python/bi_python'


def main(test_list_file):

    # if argument passed in for test list file  
    if test_list_file != None:
        test_list = bi_python_home + "/launcher/" + test_list_file
    else:
        # default
        test_list =  bi_python_home + "/launcher/test_list.txt"



    tests_dir =  bi_python_home + "/tests/"
    log_dir =bi_python_home + "/logs/"
    month = '%Y%m'
    day = '%d'
    try :
        for test_to_run in open(test_list):
            sub_dir=os.path.dirname(test_to_run) 
            test_filename = os.path.basename(test_to_run)

            # Create log directory for date if doesn't exist
            cur_log_dir = log_dir + time.strftime(month, time.localtime()) + '/' + time.strftime(month+day, time.localtime()) + '/' + sub_dir 
            if not os.path.exists(cur_log_dir):
                print "creating cur_log_dir "  + cur_log_dir
                os.makedirs(cur_log_dir)


            full_path = string.rstrip(tests_dir + test_to_run)
            #print ' full_path is "' + full_path + '"'
            cmd = 'python ' + full_path


            if os.path.isfile(full_path) == True :
                print'\n'*2
                print "Processing file " + test_to_run,

                log_timestamp = time.strftime("%Y%m%d_%H%M%S", time.localtime())
                log_filename = cur_log_dir + '/' + string.rstrip(test_filename) + "_" + log_timestamp + '.log' 
                print 'log file to use is' + log_filename
                # Redirect stdout and stderr to logfile
                cmd = string.rstrip(cmd) + ' > ' + log_filename
                popen_obj = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                (stdout, stderr) = popen_obj.communicate()
                print 'executing: ' + cmd
                print 'stderr is: ', stderr





            # if need to debug , remove stream redirection &> above and print stdout, stderr

        #else :
            #print 'ignoring ' , full_path



except IOError, err:
    print str(err) 
    sys.exit(2)

除了一件事之外,该代码还可以正常工作。当我启动子进程时,它似乎默认在 Windows 上执行。由于测试使用了一个名为 pexpect 的模块,因此我需要在 linux 上执行它。

我一直认为必须有一个简单的解决方案,但到目前为止我没有运气。

任何帮助将不胜感激,

谢谢

【问题讨论】:

  • 我不太明白这个问题。是不是在linux中执行不了的问题?什么错误?
  • 是的,这基本上就是问题所在。如果我在 linux cli 甚至 cygwin 中启动文件,它工作正常。但是当我从应用程序中尝试它时,我收到错误:“找不到关键模块。可能这个操作系统不支持它。Pexpect 适用于类 UNIX 操作系统。”
  • "当我启动子进程时,它似乎默认在 Windows 上执行。" - 你能详细说明一下这句话背后的原因吗?

标签: python linux django django-views


【解决方案1】:

这里没有足够的信息来给出正确的答案,但这里的一个常见问题是 Web 前端(例如 apache)以不同于您运行的用户的身份运行。因此,Web 处理程序无权准备您的主目录中的文件。它可能还缺少一些重要的环境变量,例如 HOME。

首先检查一下。网络服务器日志说明了什么?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-30
    • 2019-05-19
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多