How to display unicode with the CGI module?
coldpizza
vriolk at gmail.com
Sat Nov 24 18:58:56 EST 2007
Hi!
I am using the built-in Python web server (CGIHTTPServer) to serve
pages via CGI.
The problem I am having is that I get an error while trying to display
Unicode UTF-8 characters via a Python CGI script.
The error goes like this: "UnicodeEncodeError: 'ascii' codec can't
encode character u'\u026a' in position 12: ordinal not in range(128)".
My question is: (1 ) how and (2) where do I set the encoding for the
page?
I have tried adding <meta http-equiv="content-type" content="text/
html; charset=utf-8" /> but this does not seem to help, as this is an
instruction for the browser, not for the webserver and/or CGI script.
Do I have to set the encoding in the server script? On in the Python
CGI script?
The data that I want to display comes from a sqlite3 database and is
already in Unicode format.
The webserver script looks like this:
[code]
#
import CGIHTTPServer, BaseHTTPServer
httpd=BaseHTTPServer.HTTPServer(('',8080),
CGIHTTPServer.CGIHTTPRequestHandler)
httpd.serve_forever()
#
[/code]
A simplified version of my Python CGI script would be:
[code]
import cgi
print "text/html"
print
print "<html>"
print " <body>"
print "my UTF8 string: Français 日本語 Español Português Română"
print " </body>"
print "</html>"
[/code]
Where and what do I need to add to these scripts to get proper display
of UTF8 content?
More information about the Python-list
mailing list