scarblac at pino.selwerd.nl
Mon May 14 14:18:57 CEST 2001
Jeff Davis <jdavis at empires.org> wrote in comp.lang.python:
> I have a webserver running mod_python and I would like it to execute
> python scripts (kind of like mod_perl or php). I basically made the python
> handler script call execfile(req.filename). I am not sure this is the best
> Anyway, when inside the python script that is called by execfile you
> have to use req.write() to print stuff. Is there a way to access the print
> statement directly so that when someone says print it calls req.write
> rather than printing to a file object?
> Any advice here would be appreciated, I am certainly not confident
> that I am on the right track.
Redirect stdout, like this:
oldstdout = sys.stdout
sys.stdout = req
sys.stdout = oldstdout
Print calls sys.stdout.write, which is now redirected to req.write. The try:
finally: is necessary to make sure the old stdout is reinstated, even if an
That would work. I don't know how sensible using execfile is, either.
More information about the Python-list