cgi integration question

Gerhard Häring gerhard.haering at
Fri Oct 11 14:22:13 CEST 2002

Ken <ken at> [2002-10-11 12:03 GMT]:
> Hi there, I have a question on integration with python and C++.
> 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.
> When I execute the python  program:

> 1. How do I make the python to initiate execution of the C++ program and pass
>    over values/arguments?
> 2. How does python know when the C++ program finished execution so it can
>    open the result file to read from?

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

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.


-- Gerhard

More information about the Python-list mailing list