Can't print Chinese to HTTP

Gnarlodious gnarlodious at gmail.com
Sat Dec 5 20:56:32 EST 2009


On Dec 5, 3:54 am, Lie Ryan wrote:

> Because of the switch to unicode str, a simple print('晉') should've
> worked flawlessly if your terminal can accept the character, but the
> problem is your terminal does not.

There is nothing wrong with Terminal, Mac OSX supports Unicode from
one end to the other.
The problem is that your code works normally in Terminal but not in a
browser.

#!/usr/bin/python
import sys, io
print("Content-type:text/plain;charset=utf-8\n\n")
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
print("晉")

The browser shows "Server error", Apache 2 reports error:

[error] [client 127.0.0.1] malformed header from script. Bad header=
\xe6\x99\x89: test.py

So far every way to print Unicode to a browser looks very un-Pythonic.
I am just wondering if I have a bug or am missing the right way
entirely.

-- Gnarlie



More information about the Python-list mailing list