2

我无法让 python CGI 将希伯来字符打印到 Linux 上的 html 网页。这是一个演示问题的脚本:

#!/usr/bin/python3
print('Content-Type: text/html; charset=utf-8\n\n')
print ('<html><body>')
print ('first')
print ('second')
print ('תמות')
print ('third')
print ('</body></html>')

该文件以 utf-8 格式保存(不含 BOM)。我直接从浏览器地址栏调用这个 .cgi 脚本。输出是:

first second

而希伯来词和后面的任何东西都不见了。apache 日志或启用 cgitb 时未显示错误

我在 Linux ubuntu 12.04 和 centos 6 上使用 apache 2.2 和 python 3.2 以及 Firefox、chrome 和 IE 进行了测试。当然,我可以在任何普通的 html 页面上看到希伯来语。在 Windows 上它工作得很好。

编辑:虽然链接问题确实给出了最终解决方案,但这仍然不是重复的。请参阅下面的评论。

4

1 回答 1

2

看起来 sys.stdout 的默认编码不一定是 UTF-8。如果你想使用 sys.stdout.buffer.write,试试这个:

sys.stdout.buffer.write('תמות'.encode('utf-8'))
于 2012-11-17T00:04:32.620 回答