使用SSL:pexpect spawn/expect在apache上运行Django应用程序

2024-04-23 17:57:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Apache2.2上运行一个启用了SSL的django应用程序。当我没有启用SSL时,一切都很好。但是在安全性方面,操作系统如何分离进程可能有一些限制,我完全不知道。在

使用的操作系统:Solaris(不确定是否重要,但可以) Django v1.4、Apache 2.2、Python 2.7

以我的一种观点概括我正在做的事情:

def run_step(request, action):
    if (action == "action1"):
        p = subprocess.Popen("command1", 
            shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    elif (action == "action2"):
       password = mylib.getpasswd()
       ## Method 1: with pexpect
       command = pexpect.spawn('su root -c \' command2 \'')
       command.expect('[pP]assword*')
       command.sendline(password)

       ## Method 2: with subprocess and a simple command
       #command = subprocess.Popen('ls > log.file',
           shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

       ## Method 3: with subprocess stdin PIPE 
       #command = subprocess.Popen('command2', shell=True,
         stdout=subprocess.PIPE,stderr=subprocess.STDOUT,stdin=subprocess.PIPE)
       #command.stdin.write(password)
       #command.communicate()

       ## I tried with shell = False, 
       ## with stdin.flush() and communicate(password) too.

“Action1”的命令执行得非常好。但是那些需要互动的人的问题是“行动2”。 我尝试了三种不同的方法:

  • Method1:像往常一样使用pexpect发送它,但它失败了-命令根本没有执行。在
  • 方法2:尝试在没有交互的情况下调用子进程-works。在
  • 方法3:尝试使用stdin管道调用子进程以尝试交互,但再次失败。在

我不知道现在该怎么办。任何帮助都将不胜感激。在


Tags: true进程withstderrstdinstdoutactionpassword
1条回答
网友
1楼 · 发布于 2024-04-23 17:57:10

以下是对我有效的方法:

然后在python中使用一个单独的线程发送一个线程,然后使用一个单独的线程发送一个线程。在

import threading

command = 'ls -l' #some_command
t = threading.Thread(target=some_function,args=(command,))
t.start()


def some_function(command):
    some_process = pexpect.spawn(command)
    some_process.expect('[pP]assword*')
    some_process.sendline(some_password)
    some_process.expect(pexpect.EOF,timeout=1000)

我仍然不明白为什么pexpect模块不能在主线程上工作。我还是很感激你的回答,但我现在还是用这个解决方案。在

相关问题 更多 >