I have what seems to be a very simple problem yet after much searching I just cannot figure it out. I have the following code:
#!/usr/local/bin/python3
print("Content-Type: text/html; charset=utf-8")
print()
print("<html><body>Testing unicode: テスト</body></html>")
When I run it from the shell, it works fine:
# ./test.py
Content-Type: text/html; charset=utf-8
<html><body>Testing unicode: テスト</body></html>
But for some reason, nothing shows up when loading the page in a browser. My Apache error_log shows this:
[Fri Jun 03 00:11:30.079830 2016] [cgi:error] [pid 27507] AH01215: File "/var/www/html/test.py", line 4, in <module>
[Fri Jun 03 00:11:30.079849 2016] [cgi:error] [pid 27507] AH01215: print("<html><body>Testing unicode: \\u30c6\\u30b9\\u30c8</body></html>")
[Fri Jun 03 00:11:30.079872 2016] [cgi:error] [pid 27507] AH01215: UnicodeEncodeError: 'ascii' codec can't encode characters in position 29-31: ordinal not in range(128)
I even have AddDefaultCharset UTF-8
in my httpd.conf. Anyone knows what could possibly be wrong?
EDIT: What particularly pisses me off is that I tried the same code in Perl and PHP and both display fine in the browser. I thought Python 3 was always Unicode by default? I just don't know what to do here, I have fairly long amount of Python code that I can't just rewrite, or go in and manually encode/decode every string.
This is on a pretty basic CentOS 7 running on AWS.