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:
Expand|Select|Wrap|Line Numbers
- #
- import CGIHTTPServer, BaseHTTPServer
- httpd=BaseHTTPServer.HTTPServer(('',8080),
- CGIHTTPServer.CGIHTTPRequestHandler)
- httpd.serve_forever()
- #
Expand|Select|Wrap|Line Numbers
- import cgi
- print "text/html"
- print "<html>"
- print " <body>"
- print "my UTF8 string: Français 日本語 Español Português Română"
- print " </body>"
- print "</html>"
of UTF8 content?