[mod_python] mod_python and unicode

Nicolas Lehuen nicolas at lehuen.com
Sun Jun 25 09:37:16 EDT 2006


Hi,

I suppose you did it as an example, but if you've got a file encoded
in UTF-8, you don't need to decode it and reencode it. Just send the
bytes to the client, making sure that the Content-Type header mentions
that UTF-8 is used :

def handler(req):
    req.content_type = 'text/plain; charset=utf-8'
    req.sendfile(os.path.dirname(__file__) + "/myfile.txt")

Using sendfile is advertised to be the most efficient way to send a
file to the client.

Regards,
Nicolas

2006/6/25, b3nder <b3nder at yandex.ru>:
> Hi, list.
>
> Tell me please how to use utf-8 in ouput?
>
> from mod_python         import apache
> import codecs
> import os
>
>
> def handler(req):
>     req.content_type = 'text/plain; charset=utf-8'
>     req.send_http_header()
>     file = codecs.open(
>         os.path.dirname(__file__) + "/myfile.txt", "r", "utf-8")
>     a = file.read()
>     req.write(a)
>
>     return apache.OK
>
>
> gives error UnicodeEncodeError: 'ascii' codec can't encode
> characters in position 0-5: ordinal not in range(128)
>
> Thanks.
> _______________________________________________
> 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