Enter parameters into Fortran executable from python cgi script

Discussion in 'Python' started by lamar_air, Jul 11, 2003.

  1. lamar_air

    lamar_air Guest

    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
    os.system("testrun3")
    os.system("Y")
    os.system("exit")
    lamar_air, Jul 11, 2003
    #1
    1. Advertising

  2. lamar_air

    John Hunter Guest

    >>>>> "lamar" == lamar air <> writes:


    lamar> How do i do this correctly? testrun3 can be accesed from
    lamar> any dir because it's directory is set in the environment
    lamar> variables.

    If you want to interact with a program, you need to use popen2 or
    popen3, which give you access to the stdin, stdout and stderr
    (popen3).

    See this thread for a similar discussion and working example:

    http://www.google.com/groups?hl=en&...unter%40ace.bsd.uchicago.edu&lr=&num=20&hl=en


    Note, you could also expose the fortran functions to python directly
    with f2py. It is really very easy and automated

    http://cens.ioc.ee/projects/f2py2e/


    John Hunter
    John Hunter, Jul 11, 2003
    #2
    1. Advertising

  3. On 11 Jul 2003 07:34:51 -0700, (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
    >os.system("testrun3")
    >os.system("Y")
    >os.system("exit")


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

    Interactively:

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

    http://www.python.org/doc/current/lib/popen2-flow-control.html

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

    HTH

    Regards,
    Bengt Richter
    Bengt Richter, Jul 11, 2003
    #3
  4. lamar_air fed this fish to the penguins on Friday 11 July 2003 07:34 am:

    You've had other answers already, but I didn't see an explanation of
    why your logic didn't work (not for me, for you).


    > 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
    > os.system("testrun3")
    > os.system("Y")
    > os.system("exit")


    EACH os.system() call is the equivalent of entering a new command at
    the CLI/shell level. There is no linkage from one os.system() call to
    the next. Therefore, all application input has to be supplied to the
    single invocation.

    How difficult would it be to modify the Fortran to read command-line
    arguments:

    testrun3 Y exit
    ?

    Otherwise, as has been shown by others, you'll need something like
    popen2 to create a linkage so you can write the arguments to the
    application's stdin.

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Bestiaria Home Page: http://www.beastie.dm.net/ <
    > Home Page: http://www.dm.net/~wulfraed/ <
    Dennis Lee Bieber, Jul 12, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luna Moon
    Replies:
    9
    Views:
    608
    Guest
    Sep 4, 2007
  2. deadpickle
    Replies:
    1
    Views:
    978
    Jens Thoms Toerring
    Nov 7, 2010
  3. PerlFAQ Server
    Replies:
    4
    Views:
    125
    brian d foy
    Jan 3, 2011
  4. PerlFAQ Server
    Replies:
    0
    Views:
    147
    PerlFAQ Server
    Mar 30, 2011
  5. HopfZ
    Replies:
    0
    Views:
    559
    HopfZ
    Aug 28, 2006
Loading...

Share This Page