This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Unicode IO not working in cgi applet
Type: behavior Stage:
Components: Unicode Versions: Python 3.1
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: loveminix, mark.dickinson
Priority: normal Keywords:

Created on 2009-09-07 04:32 by loveminix, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg92341 - (view) Author: (loveminix) Date: 2009-09-07 04:32
The following cgi applet does output Unicode string correctly in an
Ubuntu terminal, but when invoked from a web browser (IE or Firefox) by
a client, it doesn't output the Unicode string. Output stops at
"...<div><p>", right before the Unicode string "中文".

#! /usr/bin/python3
# -*- coding: utf-8 -*-

print(
"""\
Content-type: text/html
"""
);

print(
"""\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
<body>\
"""
);

print("<div><p>");
print("中文");
print("</p></div>");

print(
"""\
</body>
</html>\
"""
);
msg92382 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-09-07 18:26
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
History
Date User Action Args
2022-04-11 14:56:52adminsetgithub: 51101
2009-09-07 18:26:53mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg92382

resolution: not a bug
2009-09-07 04:32:15loveminixcreate