[issue6852] Unicode IO not working in cgi applet
Mark Dickinson
report at bugs.python.org
Mon Sep 7 20:26:55 CEST 2009
Mark Dickinson <dickinsm at gmail.com> added the comment:
I don't think this is a Python bug; it has to do with the stdout
encoding. When connected to a terminal, sys.stdout.encoding is
(probably, on Ubuntu) UTF-8, but when you're using this as a cgi script
it's likely to be defaulting to ascii instead. Not surprisingly, Python
won't let you send non-ASCII characters to a stream whose encoding is
'ascii'.
A workaround is to use sys.stdout.buffer.write (which writes bytes, not
text), and to manually encode your unicode output in whatever encoding
you want ('utf-8', I'm assuming):
>>> sys.stdout.buffer.write('中文\n'.encode('utf8'))
中文
7
----------
nosy: +marketdickinson
resolution: -> invalid
status: open -> closed
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6852>
_______________________________________
More information about the Python-bugs-list
mailing list