running a django app on apache with SSL: pexpect spawn/expect doesn't work
Question: I'm running a django app on apache 2.2 with SSL enabled. When I don't have the
SSL enabled, all is well. But with security, there probably are some
restrictions on how an os can fork off a process, etc. I'm not aware of those
at all.

OS used: Solaris (not sure if it matters, but it could) Django v1.4, Apache
2.2, Python 2.7

An outline of what I'm doing in one of my views:

    
    
    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.
    

The command for 'Action1' gets executed very well. But the problem with those
which need interaction like that of 'Action2'. I tried three different
methods:

  * Method1: sent it using pexpect as usual but it fails - the command doesn't get executed at all.
  * Method2: tried a subprocess call wihtout an interaction - works.
  * Method3: tried a subprocess call with a stdin pipe to try interaction but failed again.

I'm not sure how to go about this now. Any help would be very highly
appreciated.


Answer: Here is what worked for me:

Spawned the pexpect call as a separate thread using the 'threading' module in
python, and then sent the password using a 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)
    

I don't still understand why the pexpect module, as is, did not work on the
main thread. Would still appreciate any answers, but I'm going with this
solution for now.

