[Tutor] Using popen

Sean 'Shaleh' Perry shalehperry@attbi.com
Tue, 02 Apr 2002 22:53:07 -0800 (PST)


> 
> 
> 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.
> 

the shell is nice and returns the real exit code to you with $?.

I copied your code into a python script and wrote the following sh script:

#!/bin/sh

read foo

exit 2

I added:

ret = p.close()
print "Return value:", ret

to your script.  Here is the output:

$ python retval.py 
Return value 512

hmm, let's try exit 1.

$ python retval.py 
Return value 256

ok, let's try exit 0.

$ python retval.py 
Return value None

ok, let's try exit 10.

$ python retval.py 
Return value 2560

and one more, exit 4.

$ python retval.py 
Return value 1024

see the pattern?  the return value is ret / 256.  If the return value was zero
then ret == None.

Hope that helps.