cgi integration question

Gerhard Häring gerhard.haering at opus-gmbh.net
Fri Oct 11 14:54:11 CEST 2002


[Broken Outlook Express quoting manually repaired, setting
textwidth to 65 to help your craps oftware not mess up]

Ken <ken at hotmail.com> [2002-10-11 12:34 GMT]:
> "Gerhard Häring" <gerhard.haering at opus-gmbh.net> wrote:
>> Ken <ken at hotmail.com> [2002-10-11 12:03 GMT]:
>> > [...] I have 2 sets of code. Python is used to display
>> > interface and C++ is used for backend execution. The
>> > executed result is saved to file so the python can read it
>> > and display back on the screen. [...]
>>
>> No need for intermediary files, just use os.popen*.
>>
>> Here's a simple example:
>>
>> >>> import os
>> >>> outf, inf = os.popen2("cat")
>> >>> print >>outf, "hi"
>> >>> outf.close()
>> >>> print inf.read()
>> hi
>>
>> outf is a stream to write to your child process (in this example,
>> the Unix cat utility, which will just duplicate on stdout what it
>> gets on stdin). After writing the "parameters" to cat's stdin, I
>> close it (don't ask me why that's necessary, but this is the way it
>> works :-D). Then, I read the whole output of cat and print it.
> 
> Hi, can you explain what is "inf" and "popen2" in:
> outf, inf = os.popen2("cat")   ?

os.popen2 is a function in the os module. It gets one parameter:
the executable you want to run as a child process. It returns a
tuple of two file objects:

* The first file object is connected to the
  input stream of the child process, i. e. you can WRITE to it.

* The second file object is connected to the output stream of the
  child process, i. e. you can READ from it.

> Is "cat" suppose to be a program name for me to call?

Yeah. As you seem to use Windows (with a b0rken newsreader), you
won't have a 'cat' program. But you will have a 'sort' program,
so you can try the following:

import os
>>> outf, inf = os.popen2("sort")
>>> print >>outf, "Mark"
>>> print >>outf, "Joe"
>>> print >>outf, "Marvin"
>>> outf.close()
>>> lines = inf.readlines()
>>> inf.close()
>>> print lines
['Joe\n', 'Mark\n', 'Marvin\n']

> So.... if I get the idea right, my program should look like this:
> import os
> outf, inf = os.popen2("cat")

# Obviously, replace 'cat' with the path to your compiled C++
# application.

# This send input to the C++ program:

> print >>outf, "hi"

> outf.close()

> print inf.read()  # does this mean  read from file?

Yep.

> and the backend c++ program look somethingl like this:
> executable filename: cat
> 
> #include <iostream>
> using namespace std;
> 
> int main (int argc, char** argv) {
>       cout << "value from argv"<< endl;
>       return 0;
> }
> 
> Still don't get how values are passed to the backend though....can you
> please explain.... : (

In the Python script, you send the paramters to the C++ program
by writing to its stdin. Then, the C++ program does its job, and
when it's ready writes its output on stdout. See above for how to
do the Python script, and here's a simplistic C++ 'backend'
program:

/** 
 * doubler.cc - Reads one line from stdin, then returns the
 * "doubled" string on stdout.
 */

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char** argv) {
    string s;

    cin >> s;

    cout << s + s;

    return 0;
}


Alternatively, you could send over commandline arguments and
parse these in the C++ app.

HTH,

-- Gerhard



More information about the Python-list mailing list