Python 3 CGI:如何输出原始字节

Python 3 CGI:如何输出原始字节

问题描述:

我决定使用Python 3让我的网站,但我遇到的Unicode输出的问题。

I decided to use Python 3 for making my website, but I encountered a problem with Unicode output.

它看起来像普通的打印( html)#html是一个 str 应该工作,但不是。我得到 UnicodeEncodeError:'ascii'编解码器不能编码字符[...]:序号不在范围(128)。这是因为网络服务器不支持unicode输出。

It seems like plain print(html) #html is astr should be working, but it's not. I get UnicodeEncodeError: 'ascii' codec can't encode characters[...]: ordinal not in range(128). This must be because the webserver doesn't support unicode output.

我尝试的下一件事是 print(html.encode('utf-8 ')),但我得到了类似的再版的字节串的输出:它被放置在 b' ...和所有的转义字符是原始形式(例如 \\\
\xd0\x9c

The next thing I tried was print(html.encode('utf-8')), but I got something like repr output of the byte string: it is placed inside b'...' and all the escape characters are in raw form (e.g. \n and \xd0\x9c)

请告诉我正确的方式输出一个Unicode( STR )字符串作为原料UTF-8编码的字节 string in Python 3.1

Please show me the correct way to output a Unicode (str) string as a raw UTF-8 encoded bytes string in Python 3.1

这里的问题是,您的标准输出不附加到实际的终端,并将使用默认情况下为ASCII编码。因此,您需要写入sys.stdout.buffer,这是sys.stdout的raw二进制输出。这可以通过各种方式来完成,最常见的似乎是:

The problem here is that you stdout isn't attached to an actual terminal and will use the ASCII encoding by default. Therefore you need to write to sys.stdout.buffer, which is the "raw" binary output of sys.stdout. This can be done in various ways, the most common one seems to be:

import codecs, sys
writer = codecs.getwriter('utf8')(sys.stdout.buffer)

和使用作者。在CGI脚本中,您可以使用写入器替换sys.stdout:

And the use writer. In a CGI script you may be able to replace sys.stdout with the writer so:

sys.stdout = codecs.getwriter('utf8')(sys.stdout.buffer)

实际上可能工作,从而就可以正常打印。试试!

Might actually work so you can print normally. Try that!