Enter parameters into Fortran executable from python cgi script

Bengt Richter bokr at oz.net
Fri Jul 11 19:56:48 CEST 2003

On 11 Jul 2003 07:34:51 -0700, lamar_air at hotmail.com (lamar_air) wrote:

>I have a fortran executable which when run from cmd it asks for a
>series of parameters which you enter then hit enter.
>From my python cgi script i want to be able to run the executable. 
>Enter the 4 or 5 parameters needed then end the executable and
>redirect to another web page which will display some results given
>from an output file from the fortran executable.
>When the user clicks submit on the form it seems to hang up on the
>interaction between python cgi and fortran exe.  In this example the
>fortran exe only accepts on variable then terminates.
>How do i do this correctly?
>testrun3 can be accesed from any dir because it's directory is set in
>the environment variables.
>import os

Using p2test.py in the role of testrun3, which I assume expects a "Y" input
and later an "exit" input (we'll talk about whether you need \n later below),
here is an example using os.popen2 to feed input to child via chin.write and
get output via chout.read:

====< p2test.py >===========================
import sys
first_inp = sys.stdin.readline()
print 'first input was:',`first_inp`
print >>sys.stderr, 'Dummy error message'
second_inp = sys.stdin.readline()
print 'second input was:',`second_inp`
1/0 # sure to make exception


 >>> import os
 >>> chin,chout,cherr = os.popen3(r'python c:\pywk\clp\p2test.py')
 >>> chin.write('Y\nexit\n')
 >>> chin.close()
 >>> print chout.read()
 first input was: 'Y\n'
 second input was: 'exit\n'

 >>> print cherr.read()
 Dummy error message
 Traceback (most recent call last):
   File "c:\pywk\clp\p2test.py", line 7, in ?
     1/0 # sure to make exception
 ZeroDivisionError: integer division or modulo by zero

There can be deadlock situations, which you can read about at


but you don't have a lot of data going to the child process, and you are not
having a continuing interchange with it, so there shouldn't be a problem.
As a first attempt, I'd just substitute 'testrun3' for r'python c:\pywk\clp\p2test.py'
in the above and see what you get.

If it doesn't work, you can experiment by removing the \n after the Y and/or the exit,
in case testrun3 is calling some single-character low level input for Y (like getch() in C).


Bengt Richter

More information about the Python-list mailing list