使用 apache 服务器时覆盖 python3 默认编码器
overwrite python3 default encoder when using apache server
我是 运行 一个 apache 服务器,它服务于一个名为 ingenious
的框架
读取带有希伯来字符的文件时得到 UnicodeDecodeError('ascii'
。
我了解到您可以使用环境变量更改 python3 的默认首选编码。
所以我使用 [setenv][3] 方法编辑了 /etc/httpd/conf/httpd.conf
:
SetEnv LC_ALL en_US.UTF-8
SetEnv LANG en_US.UTF-8
SetEnv LANGUAGE en_US.UTF-8
SetEnv PYTHONIOENCODING utf8
并使用 sudo service httpd restart
重新启动服务器,但仍然无法正常工作。
我必须声明,当软件不是 运行 apache 服务器,只是 python3 时,它可以在本地运行。
locale.getpreferredencoding()
改成ANSI_X3.4-1968
后
这里是/etc/httpd/conf/httpd.conf
的内容
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin me@domain.com
<Directory />
AllowOverride none
Require all denied
</Directory>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<IfModule mime_module>
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile On
# Supplemental configuration
LoadModule wsgi_module /usr/lib64/python3.5/site-packages/mod_wsgi/server/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so
WSGIScriptAlias / "/usr/bin/inginious-webapp.production"
WSGIScriptReloading On
Alias /static/common /usr/lib/python3.5/site-packages/inginious/frontend/common/static/
Alias /static/webapp /usr/lib/python3.5/site-packages/inginious/frontend/webapp/static/
Alias /static/lti /usr/lib/python3.5/site-packages/inginious/frontend/lti/static/
SetEnv LC_ALL en_US.UTF-8
SetEnv LANG en_US.UTF-8
SetEnv LANGUAGE en_US.UTF-8
SetEnv PYTHONIOENCODING utf8
<Directory "/usr/bin">
<Files "inginious-webapp.production">
Require all granted
</Files>
</Directory>
<DirectoryMatch "/usr/lib/python3.5/site-packages/inginious/frontend/(.+)/static/">
Require all granted
</DirectoryMatch>
IncludeOptional conf.d/*.conf
我该如何进一步调试它?
Python3 在文本模式下打开文件时使用 local.getpreferredencoding
returns 作为编码。我建议添加一些日志记录并记录默认编码值以检查是否是环境配置不正确。
可能是代码中某处存在硬编码编码参数。你能显示错误的堆栈跟踪和相应的代码吗?
根据部署机制,SetEnv 配置可能未传递给 Python。 mod_wsgi 部署见 http://ericplumb.com/blog/passing-apache-environment-variables-to-django-via-mod_wsgi.html。
与该博客 post 中的描述类似,您必须修改 inginious-webapp.production 文件以手动获取和设置环境变量。对于某些变量,这已经在默认 inginious-webapp 中完成。在这里你必须特别添加 PYTHONIOENCODING.
如果您 运行 处于 mod_wsgi 守护进程模式,这可能会有所帮助 http://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html,尤其是语言或语言环境参数。
阅读:
这解释了围绕 lang/locale 的问题。
您没有使用 mod_wsgi 守护程序模式,但您应该使用,因为守护程序模式是推荐的方法。
- http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html
- http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#embedded-or-daemon-mode
另请阅读 mod_wsgi 文档,网址为:
我是 运行 一个 apache 服务器,它服务于一个名为 ingenious
的框架读取带有希伯来字符的文件时得到 UnicodeDecodeError('ascii'
。
我了解到您可以使用环境变量更改 python3 的默认首选编码。
所以我使用 [setenv][3] 方法编辑了 /etc/httpd/conf/httpd.conf
:
SetEnv LC_ALL en_US.UTF-8
SetEnv LANG en_US.UTF-8
SetEnv LANGUAGE en_US.UTF-8
SetEnv PYTHONIOENCODING utf8
并使用 sudo service httpd restart
重新启动服务器,但仍然无法正常工作。
我必须声明,当软件不是 运行 apache 服务器,只是 python3 时,它可以在本地运行。
locale.getpreferredencoding()
改成ANSI_X3.4-1968
后
这里是/etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin me@domain.com
<Directory />
AllowOverride none
Require all denied
</Directory>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<IfModule mime_module>
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile On
# Supplemental configuration
LoadModule wsgi_module /usr/lib64/python3.5/site-packages/mod_wsgi/server/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so
WSGIScriptAlias / "/usr/bin/inginious-webapp.production"
WSGIScriptReloading On
Alias /static/common /usr/lib/python3.5/site-packages/inginious/frontend/common/static/
Alias /static/webapp /usr/lib/python3.5/site-packages/inginious/frontend/webapp/static/
Alias /static/lti /usr/lib/python3.5/site-packages/inginious/frontend/lti/static/
SetEnv LC_ALL en_US.UTF-8
SetEnv LANG en_US.UTF-8
SetEnv LANGUAGE en_US.UTF-8
SetEnv PYTHONIOENCODING utf8
<Directory "/usr/bin">
<Files "inginious-webapp.production">
Require all granted
</Files>
</Directory>
<DirectoryMatch "/usr/lib/python3.5/site-packages/inginious/frontend/(.+)/static/">
Require all granted
</DirectoryMatch>
IncludeOptional conf.d/*.conf
我该如何进一步调试它?
Python3 在文本模式下打开文件时使用 local.getpreferredencoding
returns 作为编码。我建议添加一些日志记录并记录默认编码值以检查是否是环境配置不正确。
可能是代码中某处存在硬编码编码参数。你能显示错误的堆栈跟踪和相应的代码吗?
根据部署机制,SetEnv 配置可能未传递给 Python。 mod_wsgi 部署见 http://ericplumb.com/blog/passing-apache-environment-variables-to-django-via-mod_wsgi.html。
与该博客 post 中的描述类似,您必须修改 inginious-webapp.production 文件以手动获取和设置环境变量。对于某些变量,这已经在默认 inginious-webapp 中完成。在这里你必须特别添加 PYTHONIOENCODING.
如果您 运行 处于 mod_wsgi 守护进程模式,这可能会有所帮助 http://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html,尤其是语言或语言环境参数。
阅读:
这解释了围绕 lang/locale 的问题。
您没有使用 mod_wsgi 守护程序模式,但您应该使用,因为守护程序模式是推荐的方法。
- http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html
- http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#embedded-or-daemon-mode
另请阅读 mod_wsgi 文档,网址为: