0

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.

Dendory
  • 620
  • 2
  • 10
  • 21
  • Are you sure that default python version on your system is Python 3? When you run script from shell, it will use python3 as written in shebang: `#!/usr/local/bin/python3`. But your apache can use another python version, check `ldd mod_wsgi.so`, for example, if you are using mod_wsgi. – Andrew Jun 07 '16 at 17:08
  • I don't have that module. I'm not actually sure which CGI module it runs, my 01-cgi.conf has: LoadModule cgid_module modules/mod_cgid.so LoadModule cgid_module modules/mod_cgid.so LoadModule cgi_module modules/mod_cgi.so – Dendory Jun 07 '16 at 20:37
  • Besides wouldn't Apache's CGI module just load the file using the same interpreter in the shebang? If I write a bash script and write `#!/usr/bin/bash` it will run it with bash.. – Dendory Jun 07 '16 at 20:39
  • Try `apache2ctl -M` to see which cgi module is currently enabled. – Andrew Jun 08 '16 at 04:22
  • `apachectl -M|grep cgi proxy_fcgi_module (shared) proxy_scgi_module (shared) cgi_module (shared) ` – Dendory Jun 09 '16 at 20:29
  • Solved it by adding `SetEnv PYTHONIOENCODING utf8` in Apache config. Still baffled why Python CGI defaults to ASCII when every other language I tried works just fine with unicode. Anyways... – Dendory Jun 09 '16 at 20:43

0 Answers0