cgi integration question

Gerhard Häring gerhard.haering at opus-gmbh.net
Fri Oct 11 08:22:13 EDT 2002


Ken <ken at hotmail.com> [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 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.

HTH,

-- Gerhard



More information about the Python-list mailing list