|
Jim Gallacher
jpg at jgassociates.ca
Fri May 19 08:25:23 EDT 2006
Terry Macdonald wrote:
> Graham Dumpleton wrote:
>>
>> On 19/05/2006, at 8:13 PM, Terry Macdonald wrote:
>>
>>>
>>> "sys.stdout=sys.stderr=req"
>>>
>>> This may be a dumb question but I'm not getting it...
>>> What does the above line do?
>>> Why does one want to set standard error and standard out to the
>>> request object?
>>> What does printing to the request object do?
>>> ...and how does it work?
>>>
>>> I'm missing something fundamentally object oriented here, aren't I?
>>>
>>> I just see objects that are used to print stuff and then a request
>>> object which contains request information. I'm not getting the link.
>>
>> Both sys.stderr and sys.stdout are file objects. The primary method
>> for writing
>> data is the "write()" member function. It is the "write()" member
>> function that "print"
>> calls on the sys.stdout file object.
>>
>> Outside of mod_python, you could replace sys.stdout with some other
>> file object,
>> for example an open log file, and every use of "print" in the program
>> would see that
>> output go to the log file instead.
>>
>> In practice, sys.stdout doesn't actually have to be a file object,
>> just a file like
>> object which provides a "write()" method. As a "req" object provides a
>> "write()"
>> function, it could technically thus be used as a substitute for
>> sys.stdout. As I
>> pointed out in prior email though, this would actually be a dangerous
>> thing
>> to do in mod_python where a multithreaded MPM is being used.
>>
>> Hope this makes sense.
>>
>> Graham
> Thanks for the help/reply Graham
>
> So effectively if you print something to the req object it morphs into a
> response back to the client browser?
Yes, but as Graham says, the safe way to do that in mod_python is:
print >> req, "Stuff"
which is the same as:
req.write("Stuff\n")
Jim
|