这是我的代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json,random,sys,io
PATH = "/srv/rudailynews/words.json"
print ("Content-type:text/plain;charset=utf-8\r\n\r\n")
wordsfile=open(PATH,'r')
words=json.loads(wordsfile.read())
print(random.choice(words['subject'])+" "+
random.choice(words['predicate'])+" "+
random.choice(words['word3']))
这个程序应该从json中选择3个随机短语并将它们组合起来。它通常从pycharm和控制台运行但是当我从浏览器作为CGI运行时我有一个错误
Traceback (most recent call last):
File "/srv/cgi/rudailynews/getsnt.py", line 24, in <module>
random.choice(words['word3']))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)
[Fri Oct 30 20:42:30.165448 2015] [cgid:error] [pid 10959:tid 140680244426496] [client 127.0.0.1:40434] End of script output before headers: getsnt.py
我尝试过来自here(第二个示例)的解决方案,该解决方案仅在python3中运行,并且浏览器显示空白页。
def set_output_encoding(codec, errors='strict'):
sys.stdout = io.TextIOWrapper(
sys.stdout.detach(), errors=errors,
line_buffering=sys.stdout.line_buffering)
set_output_encoding('utf8')
在日志中我有
Traceback (most recent call last):
File "/srv/cgi/rudailynews/getsnt.py", line 21, in <module>
words=json.loads(wordsfile.read())
File "/usr/lib/python3.4/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 28: ordinal not in range(128)
您可以 words.json here 。我不知道我刚刚使用 wget (直接链接here)下载了哪种编码。如何使用UTF8制作cgi输出?
编辑:我在envvars中将LANG更改为。 / etc / default / locale,在charset.conf中将PassEnv Lang添加到我的vhost配置和未注释的AddDefaultCharset UTF-8,但 sys.stdout.encoding 仍然是浏览器中的ANSI_X3.4-1968和UTF- 8在控制台。测试脚本:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
print ("Content-type:text/html\r\n\r\n")
print (sys.stdout.encoding)