CGI and Unicode

Jim Hefferon jhefferon at
Mon Jun 23 20:07:47 CEST 2003


I have been struggling with getting Unicode out of Python's cgi 
module. I have a small script illustrating the problem at the bottom 
but first I need to explain.

I want that users can send me material with a wide variety of 

I understand from looking around the net (particularly at this 
on the Python developers list) that the best that I can hope for 
is to set the page with the form on it to be showing, say UTF-8, 
and then the data should show up UTF-8 encoded at my site.  

I think I have the page set to UTF-8 encoded by following the 
recommendation on 
about the META tag.

But when I ask what is the type of the variable that I get from 
the cgi module, it comes out as StringType, not UnicodeType.  My 
browser is Galeon on the latest Debian and I've also tested it 
with IE on NT.

What am I missing?  Thanks for any help,
Jim Hefferon

-------- ----------------------------
#!/usr/bin/python -u
# test CGI unicode issue
from types import *

import cgi
import cgitb

# create the HTML document
print "Content-Type: text/html\n\n"
print "<html><head><title>CGI TEST</title></head>\n\n"
print "<META http-equiv=\"Content-Type\" content=\"text/html;
print "<body bgcolor=\"white\">\n"

    if type(var) is UnicodeType:
        print "<p>The type of the variable is a Unicode</p>\n"
    elif type(var) is StringType:
        print "<p>The type of the variable is a regular string</p>\n"
    print "<p>Character 12 is %s</p>\n" % (var[12],)

print "<form method=\"POST\" accept-charset=\"utf-8\">\n"
print "<input type=\"text\" name=\"name\">\n"
print "<input type=\"submit\">\n"
print "</form>\n"
print "</body></html>"

More information about the Python-list mailing list