[Tutor] Using popen

Sheila King sheila@thinkspot.net
Tue, 2 Apr 2002 22:34:04 -0800


Well, I hope someone can give me some advice here.

I'm writing a cgi script that needs to call a shell command and get 
the output. Retrieving the exit code of the command would be fine. 
The syntax of the statement is this:

(we're talking Linux...bash shell...)

vauthenticate ACCOUNTNAME

and after this, it reads a password from stdin and then determines if 
that is the correct password for the POP email account ACCOUNTNAME.

If it is correct, it returns some string data plus an exit code of 
zero.

If it is not correct, it returns an exit code of 1 and not data.

I tested it at the command line with the following results:
(first, a successful password request)

[username@servername:~ ]$ vauthenticate secret_test
secret
UID=41121
GID=41121
USER=xaccountname
HOME=/big/dom/xaccountname
MAILDIR=./vmspool/secret_test
VUSER=secret_test

[username@servername:~ ]$ echo $?
0


(and now with a bad password)

[thinker@FQ-Nine:~ ]$ vauthenticate secret_test
badpass

[thinker@FQ-Nine:~ ]$ echo $?
1

OK, so you can see the shell syntax.

I figured, I'd use popen, 
issue the command 'vauthenticate ACCOUNTNAME'
and then write the proposed password to the pipe.
Then, according to the documentation, when I close the pipe, it 
should return the exit code of the command. Unfortuntately I did not 
get the results I expected. Here is the session, using same good 
account and good password as above:


Python 2.2 (#1, Feb 21 2002, 02:25:03)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more 
information.
>>> import os
>>> p = os.popen('vauthenticate secret_test', 'w', 0)
>>> p.write('secret')
>>> p.close()
256
>>>

Now, I don't really understand these pipes and popen as well as I'd 
like, so I may well be doing something really stupid here. But, I 
certainly did not expect to get an exit code of 256 with this 
particular ACCOUNTNAME/password pair, as it returns an exit code of 
zero at the command line (and I know these are correct values for one 
of my POP accounts...).

Advice...? Please? I'd be most grateful.

Thanks,

Sheila King
http://www.thinkspot.net/sheila/