Here is my code
#!/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']))
This program should choose 3 random phrases from json and combine them. It runs from pycharm and from console normally but when i run it from a browser as a CGI i have an error
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
I've tried solution from here (second example) which only runs in python3, and browser is showing a blank page.
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')
and in logs i have
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)
You can get words.json here. I don't know which encoding it is i just downloaded it using wget (direct link here). How to make cgi make output in UTF8?
edit: I changed LANG in envvars to . /etc/default/locale, added PassEnv Lang to my vhost config and uncommented AddDefaultCharset UTF-8 in charset.conf, but sys.stdout.encoding is still ANSI_X3.4-1968 in browser and UTF-8 in console. Test script:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
print ("Content-type:text/html\r\n\r\n")
print (sys.stdout.encoding)