(Fwd) Re: [mod_python] How to pass values

Graham Dumpleton grahamd at dscpl.com.au
Sat Jan 27 06:26:09 EST 2007


There is 4 seconds between the filter call and the handler call. That  
seems a bit
long. Are you sure they come from the same request?

In both filter and handler also log the following attributes:

   req.method
   req.uri
   req.connection.id

For the filter, prefix with 'filter.' to get to req object first  
obviously.

You might also log result of os.getpid() to verify that each is run  
in the same
process.

Anyway, I suspect that it isn't from the same request. What is  
missing here and
you need to understand is that the input filters for a request are  
not actually
called until the handler calls req.read() or one of the other read  
methods of the
request object. It is only after req.read() is called that the  
req.myflag value would
have been set. So, if you haven't called req.read() before accessing  
the attribute
it will fail.

Graham

On 27/01/2007, at 8:01 PM, export at hope.cz wrote:

>  Graham,
> Thank you for your reply.
> So, I used the debugging  techniques like this:
>
> in input filter
> ###########################
> from mod_python import apache,Cookie
> def inputfilter(filter):
> 	filter.req.log_error('Running inputfilter')
> 	filter.req.log_error('Setting myflag..')
> 	filter.req.myflag = True
> 	filter.req.log_error('myflag was set to:')
> 	filter.req.log_error(`filter.req.myflag`)
> 	if filter.req.method != 'POST':
> 		filter.pass_on()
> 		return
> 	s = filter.read()
> 	while s:
> 		f.write(s)
> 		f.flush()
> 		filter.write(s)
> 		filter.flush()
> 		s = filter.read()
> 		
> 	if s is None:
> 		filter.close()
> 		f.close()
> ########################
>
> in handler
> ##########################
>
> def handler(req):
>      if req.method == 'GET':
> 	......
>
>     else:
> 	if  req.method == 'POST':
> 	 	req.log_error('Myhandler')
> 		req.log_error('MyHandler, in POST request, has myflag')
> 		req.log_error(`req.myflag`)
> 	....
> 	....
> #######################
> and here is a part of error log
>
> ..
> [Sat Jan 27 09:48:54 2007] [notice] Child 2196: Starting thread to  
> listen on port 80.
> [Sat Jan 27 09:49:05 2007] [error] [client 127.0.0.1] Running  
> inputfilter
> [Sat Jan 27 09:49:05 2007] [error] [client 127.0.0.1] Setting myflag..
> [Sat Jan 27 09:49:05 2007] [error] [client 127.0.0.1] myflag was  
> set to:
> [Sat Jan 27 09:49:05 2007] [error] [client 127.0.0.1] True
> [Sat Jan 27 09:49:05 2007] [error] [client 127.0.0.1] Running  
> inputfilter
> [Sat Jan 27 09:49:05 2007] [error] [client 127.0.0.1] Setting myflag..
> [Sat Jan 27 09:49:05 2007] [error] [client 127.0.0.1] myflag was  
> set to:
> [Sat Jan 27 09:49:05 2007] [error] [client 127.0.0.1] True
> [Sat Jan 27 09:49:08 2007] [error] [client 127.0.0.1] Running  
> inputfilter
> [Sat Jan 27 09:49:08 2007] [error] [client 127.0.0.1] Setting myflag..
> [Sat Jan 27 09:49:08 2007] [error] [client 127.0.0.1] myflag was  
> set to:
> [Sat Jan 27 09:49:08 2007] [error] [client 127.0.0.1] True
> [Sat Jan 27 09:49:08 2007] [error] [client 127.0.0.1] Running  
> inputfilter
> [Sat Jan 27 09:49:08 2007] [error] [client 127.0.0.1] Setting myflag..
> [Sat Jan 27 09:49:08 2007] [error] [client 127.0.0.1] myflag was  
> set to:
> [Sat Jan 27 09:49:08 2007] [error] [client 127.0.0.1] True
> [Sat Jan 27 09:49:12 2007] [error] [client 127.0.0.1] Myhandler,  
> referer:
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 09:49:12 2007] [error] [client 127.0.0.1] MyHandler, in  
> POST request, has myflag,
> referer: http://127.0.0.1/mod/uploadvideo.py?17644
> .....
> ....
> ....
> [Sat Jan 27 09:49:12 2007] [error] [client 127.0.0.1]  
> AttributeError: 'mp_request' object has no
> attribute 'myflag', referer: http://127.0.0.1/mod/uploadvideo.py?17644
> ...
>
>
> Where can be a problem? What shall I try?
> Can you see any mistake I make?
> Thank you again for help
> Lad
>
>
>> Use some standard debugging techniques yourself to try and work it  
>> out.
>>
>> On 26/01/2007, at 10:44 PM, export at hope.cz wrote:
>>
>>> Graham,
>>> Thank you for your reply.
>>> I have
>>>
>>> def inputfilter(filter):
>>> filter.req.myflag = True
>>
>> Add:
>>
>>    filter.req.log_error('inputfilter')
>>
>>> ...
>>> ...
>>>
>>> so, as you can see the first command in input filter
>>> is filter.req.myflag = True
>>> but yet, I can not access the req.myflag
>>> in my request handler.
>>> When I try
>>> req.write(`req.myflag`)
>>
>> Add before this:
>>
>>    req.log_error('handler')
>>
>> In other words, use the logging system to dump information into the
>> Apache
>> log files to see if the input filter is actually being called. It
>> would look like it
>> isn't. What have you done so far to confirm it is?
>>
>> Graham
>>
>>> I will receive the same error:
>>>
>>> AttributeError: 'mp_request' object has no attribute 'myflag'
>>>
>>> What shall I try to solve the problem. Do you have any idea?
>>> Thank you
>>> Lad
>>>
>>>> Your input filter can't have been called then, or not the part
>>>> where you
>>>> assigned the attribute. If the variable may not be there in all
>>>> situations,
>>>> then use:
>>>>
>>>> if hasattr(req, 'myflag'):
>>>> if req.myflag:
>>>> ..
>>>>
>>>> Graham
>>>>
>>>> On 26/01/2007, at 7:09 PM, export at hope.cz wrote:
>>>>
>>>>> Graham ,
>>>>> Thank you for your reply and help.
>>>>> I tried the way you suggested but I received the error
>>>>>
>>>>> AttributeError: 'mp_request' object has no attribute 'myflag'
>>>>>
>>>>> Can you please help?
>>>>> Thank you
>>>>> Lad.
>>>>>
>>>>>
>>>>>>
>>>>>> On 26/01/2007, at 2:45 AM, export at hope.cz wrote:
>>>>>>
>>>>>>> Depending on a value being calculated in input filter,
>>>>>>> I will have to make a decission in request handler.
>>>>>>> What is a way to find out the value of the variable from input
>>>>>>> filter in request handler?
>>>>>>
>>>>>> I believe this was mentioned before.
>>>>>>
>>>>>> In filter, set an attribute on the request object:
>>>>>>
>>>>>> def inputfilter(filter):
>>>>>> ...
>>>>>> filter.req.myflag = True
>>>>>> ...
>>>>>>
>>>>>> In the request handler, you can then access that variable.
>>>>>>
>>>>>> def handler(req):
>>>>>> ...
>>>>>> if req.myflag:
>>>>>> ...
>>>>>> ...
>>>>>>
>>>>>> Graham
>>>>>
>>>>
>>>>
>>>
>>>
>>> ------- End of forwarded message -------
>


More information about the Mod_python mailing list