子进程和硬编码目录工作正常,变量失败

发布于 2024-10-31 08:21:53 字数 2318 浏览 0 评论 0原文

所以我遇到了一个问题,现在确定我做错了什么。我正在使用 python 2.4 和 subprocess 模块。这与我的 django 网站集成。我有一个要求一些用户输入的表单,它将其放入一个文件中,然后使用文件所在的目录启动脚本。

如果我对字符串中的目录进行硬编码,它将正常工作,并输出 wlst脚本,如果我不这样做,它将无法工作,只是连接并认为它已完成。

仍然没有任何信息。说说为什么。该脚本可以在正常的命令行中正常工作,甚至可以在正常的 python 代码中正常工作。我唯一能说的就是变量。

代码:

if form.is_valid():
                        env = form.cleaned_data['env']
                        user = form.cleaned_data['user']
                        group = form.cleaned_data['group']
                        role = form.cleaned_data['role']

                        id = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))

                        root = "/opt/tools/Mapper/output/webui/%s" % (id)

                        if not os.path.exists(root):
                                os.makedirs(root)

                        output = open(root+"/mapping.out", 'w')

                        if (user):
                                user_stg = "User:"+user+":"
                                password = User.objects.filter(user__contains=user).values('password')

                                password_f = open(root+"/passwords.out", 'w')

                                password_out = "%s:%s" % (user, password[0]['password'].encode('ascii'))

                                password_f.write(password_out)
                        if (group):
                                grp_stg = "GroupName:"+group+":"
                        if(role):
                                role_stg = "RoleName:"+role

                        out_stg = user_stg+grp_stg+role_stg

                        output.write(out_stg+"\n")

                        command = "/opt/tools/Mapper/bin/wlst /opt/tools/Mapper/bin/creator -e ldap.dev -s pacdcespopsa1:7001 -o /opt/tools/Mapper/output/webui/JBHSMN"

                        args = shlex.split(command)

                        print args

                        resp = HttpResponse(command_output(args));

def command_output(args):
    import subprocess
    return subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]

此代码与变量驱动代码之间的唯一区别是我有 for

command = "/opt/tools/Mapper/bin/wlst /opt/tools/Mapper/bin/creator -e ldap.dev -s pacdcespopsa1:7001 -o %s" % (root)

So am running into an issue and now sure what I am doing wrong. I am using python 2.4 and the subprocess module. This is integrated with my django site. I have a form that askes for some user input, it puts that into a file then starts the script with the directory the file(s) are in.

If I hardcode the directory in the string it will work fine, and out put the wlst script, if I do not it will not work and just connected and think that it is done.

Still stuck with no information. Onto why. The script works fine normal command line, and even from normal python code. The only thing I could tell is with the variables.

Code:

if form.is_valid():
                        env = form.cleaned_data['env']
                        user = form.cleaned_data['user']
                        group = form.cleaned_data['group']
                        role = form.cleaned_data['role']

                        id = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))

                        root = "/opt/tools/Mapper/output/webui/%s" % (id)

                        if not os.path.exists(root):
                                os.makedirs(root)

                        output = open(root+"/mapping.out", 'w')

                        if (user):
                                user_stg = "User:"+user+":"
                                password = User.objects.filter(user__contains=user).values('password')

                                password_f = open(root+"/passwords.out", 'w')

                                password_out = "%s:%s" % (user, password[0]['password'].encode('ascii'))

                                password_f.write(password_out)
                        if (group):
                                grp_stg = "GroupName:"+group+":"
                        if(role):
                                role_stg = "RoleName:"+role

                        out_stg = user_stg+grp_stg+role_stg

                        output.write(out_stg+"\n")

                        command = "/opt/tools/Mapper/bin/wlst /opt/tools/Mapper/bin/creator -e ldap.dev -s pacdcespopsa1:7001 -o /opt/tools/Mapper/output/webui/JBHSMN"

                        args = shlex.split(command)

                        print args

                        resp = HttpResponse(command_output(args));

def command_output(args):
    import subprocess
    return subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]

The only difference between this code and the varable driven one is that I have for

command = "/opt/tools/Mapper/bin/wlst /opt/tools/Mapper/bin/creator -e ldap.dev -s pacdcespopsa1:7001 -o %s" % (root)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文