python-2.3 apache2 hi,
I'm trying to use unicode function into a cgi script with no success. #-------- cedric.py--- #!/usr/bin/python import os,sys import cgi import cgitb; cgitb.enable() print "Content-Type: text/html\ "+os.linesep+"\ "+os.linesep+"<TITLE>hipo'potame</TITLE>" sys.stdout.flush() content = u'c\xe9dric' print content
print uses sys.stdout.encoding to encode unicode strings. You can check what this is in your cgi with import sys print sys.stdout.encoding
I think it will work to explicitly encode the unicode string in the encoding you want for the web page. Try
print content.encode(xxx)
where xxx is the encoding you want, for example 'utf-8' or 'latin-1'.
Also I think you should use an explicit '\r\n' instead of os.linesep, HTTP specifies an explicit CRLF.
Finally, you might want to put a charset tag in your Content-Type header, e.g. print "Content-Type: text/html;charset=utf-8\ or whatever. Obviously the charset you specify here should match what you use in the encode().
Kent
#---------------------
when I invoke it from my xterm, I get:
python cedric.py
Content-Type: text/html
<TITLE>hipo'potame</TITLE> cédric
but the same does not work on apache:
/home/system/briner/obsadminwww/cedric.py 6 "+os.linesep+"\
7 "+os.linesep+"<TITLE>hipo'potame</TITLE>"
8 sys.stdout.flush()
9 content = u'c\xe9dric'
10 print content
content = u'c\xe9dric'
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
position 1: ordinal not in range(128) args = ('ascii', u'c\xe9dric', 1, 2, 'ordinal not in range(128)') encoding = 'ascii' end = 2 object = u'c\xe9dric' reason = 'ordinal not in range(128)' start = 1
As if the stdout as to pbe in ascii??
any idea on how to do this
Cedric BRINER
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor