首页 文章

使用SSL在apache上运行django应用程序:pexpect spawn / expect不起作用

提问于
浏览
0

我在启用了SSL的apache 2.2上运行了一个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'的命令执行得非常好 . 但那些需要互动的问题就像'Action2'那样 . 我尝试了三种不同的方法:

  • 方法1:像往常一样使用pexpect发送它但它失败了 - 命令根本没有执行 .

  • 方法2:尝试了一个没有交互的子进程调用 - 工作 .

  • 方法3:尝试使用stdin管道进行子进程调用以尝试交互,但再次失败 .

我现在不确定如何解决这个问题 . 任何帮助将非常感谢 .

1 回答

  • 0

    这对我有用:

    使用python中的'threading'模块将pexpect调用作为一个单独的线程生成,然后使用sendline发送密码 .

    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模块不能在主线程上工作 . 仍然会欣赏任何答案,但我现在正在使用这个解决方案 .

相关问题