Unicode in cgi-script with apache2

Peter Otten __peter__ at web.de
Sun Aug 17 09:12:30 EDT 2014


Dominique Ramaekers wrote:

> As I suspected, if I check the used encoding in wsgi I get:
> ANSI_X3.4-1968
> 
> I found you can define the coding of the script with a special comment:
> # -*- coding: utf-8 -*-
> 
> Now I don't get an error but my special chars still doesn't display well.
> The script:
> # -*- coding: utf-8 -*-
> import sys
> def application(environ, start_response):
>      status = '200 OK'
>      output = 'Hello World! é ü à ũ'
>      #output = sys.getfilesystemencoding() #1
> 
>      response_headers = [('Content-type', 'text/plain'),
>                          ('Content-Length', str(len(output)))]
>      start_response(status, response_headers)
> 
>      return [output]
> 
> Gives in the browser as output:
> 
> Hello World! é ü à ũ

That's UTF-8 interpreted as Latin-1. Try specifying the charset in the 
header:

...
response_headers = [('Content-type', 'text/plain; charset=utf-8'),
...
 
> And if I check the encoding with the python script (uncommenting line
> #1), I still get ANSI_X3.4-1968





More information about the Python-list mailing list