python - 如何从 Django Web 应用程序中在 linux shell 上启动 python 子进程命令?

标签 python linux django django-views

我有一个网络应用程序,它读取并显示由 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 上执行。

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

如有任何帮助,我们将不胜感激,

谢谢

最佳答案

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

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

关于python - 如何从 Django Web 应用程序中在 linux shell 上启动 python 子进程命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3750181/

相关文章:

php - 使用 django 在 mysql 中查看波斯语/阿拉伯语数据

python从特定行位置的数据帧收集数据

python - Scrapy 传递参数并写入 MySQL

linux - 如何在 linux 中的 tcsh shell 上执行此操作

linux - 使用 `su - user c` 在 Linux 中以另一个用户身份运行命令会创建一个重复进程

python - django.core.exceptions.ImproperlyConfigured : Error loading MySQLdb module: No module named 'MySQLdb'

c++ - 我如何将 C++ 和 Python 与 SWIG 集成

python - 修复 Pelican RSS Feed 中的验证错误

python - Pytorch:我们可以直接在 forward() 函数中使用 nn.Module 层吗?

linux - Tomcat7 绑定(bind)到 shell