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

Jim Gallacher jpg at jgassociates.ca
Sat Jan 27 11:19:46 EST 2007


export at hope.cz wrote:
> Graham ,
> Thank you again  for your help.
> So I added .both to input filter and handler
>   req.method
>   req.uri
>    req.connection.id
> Now they looks like this:
> 
> ###############
> 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`)
> 	filter.req.log_error('inputfilter-filter.req.method')
> 	filter.req.log_error(`filter.req.method`)
> 	filter.req.log_error('inputfilter-filter.req.uri:')
> 	filter.req.log_error(`filter.req.uri`)
> 	filter.req.log_error('inputfilter-filter.req.connection.i.d')
> 	filter.req.log_error(`filter.req.connection.id`)
> 	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()
> ###############
> and a part of handler looks like this
> :
> ##############
> else:
> 	if  req.method == 'POST':
> 	 	req.log_error('Myhandler')
> 		req.log_error('MyHandler, in POST request, has myflag')
> 		req.log_error('myhandler-req.method')
> 		req.log_error(`req.method`)
> 		req.log_error('myhandler-req.uri')
> 		req.log_error(`req.uri`)
> 		req.log_error('myhandler-req.connection.id')
> 		req.log_error(`req.connection.id`)
> 	
> 		req.log_error(`req.myflag`)
> 		
> ###############
> 
> and the error log is here
> ##################
> [Sat Jan 27 13:01:33 2007] [notice] Apache/2.2.4 (Win32) configured -- resuming normal 
> operations
> [Sat Jan 27 13:01:33 2007] [notice] Server built: Jan  9 2007 23:17:20
> [Sat Jan 27 13:01:33 2007] [notice] Parent: Created child process 1864
> [Sat Jan 27 13:01:33 2007] [notice] mod_python: Creating 8 session mutexes based on 0 
> max processes and 250 max threads.
> [Sat Jan 27 13:01:34 2007] [notice] Child 1864: Child process is running
> [Sat Jan 27 13:01:34 2007] [notice] Child 1864: Acquired the start mutex.
> [Sat Jan 27 13:01:34 2007] [notice] Child 1864: Starting 250 worker threads.
> [Sat Jan 27 13:01:34 2007] [notice] Child 1864: Starting thread to listen on port 80.
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] Running inputfilter
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] Setting myflag..
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] myflag was set to:
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] True
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] inputfilter-filter.req.method
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] 'GET'
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] inputfilter-filter.req.uri:
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] '/mod/uploadvideo.py'
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] inputfilter-filter.req.connection.i.d
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] 236
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] Running inputfilter
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] Setting myflag..
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] myflag was set to:
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] True
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] inputfilter-filter.req.method
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] 'GET'
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] inputfilter-filter.req.uri:
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] '/mod/uploadvideo.py'
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] inputfilter-filter.req.connection.i.d
> [Sat Jan 27 13:01:41 2007] [error] [client 127.0.0.1] 236
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] Myhandler, referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 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 13:01:48 2007] [error] [client 127.0.0.1] myhandler-req.method, referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] 'POST', referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] myhandler-req.uri, referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] '/mod/uploadvideo.py', referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] myhandler-req.connection.id, referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] 236, referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] mod_python (pid=1864, 
> interpreter='www.emim.cz', phase='PythonHandler', handler='uploadvideo'): Application error, 
> referer: http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] ServerName: 'www.emim.cz', referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] DocumentRoot: 'C:/Program 
> Files/Apache Software Foundation/Apache2.2/htdocs', referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] URI: '/mod/uploadvideo.py', referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] Location: '/mod', referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] Directory: None, referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] Filename: 'C:/Program Files/Apache 
> Software Foundation/Apache2.2/htdocs/mod', referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] PathInfo: '/uploadvideo.py', referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1] Traceback (most recent call last):, 
> referer: http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1]   File "C:\\PYTHON23\\Lib\\site-
> packages\\mod_python\\importer.py", line 1537, in HandlerDispatch\n    
> default=default_handler, arg=req, silent=hlist.silent), referer: 
> http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1]   File "C:\\PYTHON23\\Lib\\site-
> packages\\mod_python\\importer.py", line 1229, in _process_target\n    result = 
> _execute_target(config, req, object, arg), referer: http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1]   File "C:\\PYTHON23\\Lib\\site-
> packages\\mod_python\\importer.py", line 1128, in _execute_target\n    result = object(arg), 
> referer: http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 2007] [error] [client 127.0.0.1]   File 
> "C:/Django/projekty/mimiproject/apps/mimi/mod\\uploadvideo.py", line 69, in handler\n    
> req.log_error(`req.myflag`), referer: http://127.0.0.1/mod/uploadvideo.py?17644
> [Sat Jan 27 13:01:48 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
> ################################
> 
> Can you see any problem there?
> Thank you for help and reply
> Lad.
> 
> 
> 
>> 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?

As Graham pointed out you need a call to req.read() or create a 
FieldStorage instance which will do that internally. When I take your 
code snippet and call util.FieldStorage(req) it works just fine.

Jim


>>> 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 -------
> 
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
> 



More information about the Mod_python mailing list