[mod_python] [help]please give an input filter example ?

David Fraser davidf at sjsoft.com
Thu Sep 18 18:02:34 EST 2003


camge wrote:

>----- Original Message ----- 
>From: "David Fraser" <davidf at sjsoft.com>
>To: "camge" <is83070 at cis.nctu.edu.tw>
>Cc: <mod_python at modpython.org>
>Sent: Thursday, September 18, 2003 7:05 PM
>Subject: Re: [mod_python] [help]please give an input filter example ?
>
>
>  
>
>>camge wrote:
>>
>>    
>>
>>>----- Original Message ----- 
>>>From: "David Fraser" <davidf at sjsoft.com>
>>>To: "camge" <is83070 at cis.nctu.edu.tw>
>>>Sent: Thursday, September 18, 2003 6:12 PM
>>>Subject: Re: [mod_python] [help]please give an input filter example ?
>>>
>>>
>>>
>>>
>>>      
>>>
>>>>>camge wrote:
>>>>>
>>>>>Hi ,
>>>>>
>>>>>  I have written an output filter, and it works fine.
>>>>>But I dont know how to write an input filter, there are not any
>>>>>          
>>>>>
>examples.
>  
>
>>>>>I have tried to write an simple input filter according to an output
>>>>>
>>>>>
>>>>>          
>>>>>
>>>filter.
>>>
>>>
>>>      
>>>
>>>>>But it always makes IE hang there.
>>>>>If any one can give me an example about input filter?
>>>>>Thanks a lot!
>>>>>
>>>>>My environment:
>>>>>Apache/2.0.44 (Win32)
>>>>>mod_ssl/2.0.44
>>>>>OpenSSL/0.9.7
>>>>>mod_python/3.0.3
>>>>>Python/2.2.2
>>>>>
>>>>>Best regards,
>>>>>camge
>>>>>
>>>>>
>>>>>
>>>>>          
>>>>>
>>>>Why don't you send your example input filter that makes IE hang?
>>>>
>>>>Thanks
>>>>David
>>>>
>>>>
>>>>
>>>>
>>>>        
>>>>
>>>Below is my simple input filter inputFilter.py:
>>>-------------------------------------------------------------
>>>def inputfilter(filter):
>>>   f=open('filter.log','ab')#for debug
>>>   data='start'
>>>   while data!=None:
>>>       try:
>>>           data = filter.read()
>>>           if data:
>>>               filter.write(data)
>>>               f.write(str(data))
>>>       except Exception,e:
>>>               f.write(str(e))
>>>               data=None
>>>    if data==None:
>>>       filter.close()
>>>    f.close()
>>>----------------------------------------
>>>httpd.conf:
>>>----------------------------------------
>>><Location /test/>
>>>    PythonInputFilter inputFilter MyInputFilter
>>>    SetInputFilter MyInputFilter
>>></Location>
>>>
>>>
>>>
>>>
>>>      
>>>
>>Excuse me rearranging your post, bottom posting makes discussion easier...
>>
>>I think the problem here is that you're doing filter.read() which will
>>read until an end of file, which will never happen on an input filter.
>>You probably need a more intelligent way of working out when to stop
>>reading, e.g. for HTTP, you could read a line at a time.
>>What are you wanting to use the input filter for?
>>
>>David
>>
>>
>>
>>    
>>
>Thanks for your fast response!
>I have tried filter.readline(), but it still reads nothing.
>  
>
That's a pleasure. OK, I don't really know anything about input filters, 
I was just pretending... maybe some else can help

>I need input filter to convert character set of form data submitted by cell
>phone browser.
>For example, convert utf-8 to big5 or gb.
>If I can do it in input filter, then I dont need to modify all of my pages.
>  
>
Aha. What I would do is just put a function in a module that changes the 
encoding.
Get the arguments like you normally would, then just do a decode/encode.
Since that module can be accessed from anywhere, it will work just as well.
Also, it's less likely to cause bugs, and will probably be faster 
(you'll only making one jump into the Python interpreter and back)

Cheers
David




More information about the Mod_python mailing list