感觉这里有点疯狂。我已经使用 mod_wsgi 设置了 Apache,但我无法使编码正常工作。我有:
- 测试 mod_wsgi 是否在守护进程模式下运行
- read 格雷厄姆·邓普尔顿的博客文章 http://blog.dscpl.com.au/2014/09/setting-lang-and-lcall-when-using.html关于设置
lang
and locale
设置为WSGIDaemonProcess
指示。
- 创建了一个似乎可以证明问题的最小测试
# I recompiled the mod_wsgi file to get the Python version correct
sys.version = '3.8.6 (default, Sep 24 2020, 21:54:23) \n[GCC 8.3.0]'
sys.prefix = '/usr/local'
sys.path = ['/usr/local/lib/python38.zip', '/usr/local/lib/python3.8', '/usr/local/lib/python3.8/lib-dynload', '/usr/local/lib/python3.8/site-packages', '/usr/local/src/scorched']
# This seems to be a timing thing? Not sure, but possibly problematic
locale.getlocale() = (None, None)
# This was fixed by setting lang or locale (not sure which)
locale.getdefaultlocale() = ('en_US', 'UTF-8')
sys.getdefaultencoding() = 'utf-8'
# These seem like a problem...
sys.getfilesystemencoding() = 'ascii'
locale.getpreferredencoding(False): 'ANSI_X3.4-1968'
# It's daemon mode
mod_wsgi.process_group = 'cl'
我的 WSGI 配置如下所示:
WSGIScriptAlias / /opt/courtlistener/docker/apache/wsgi-configs/python_version_test.py
WSGIDaemonProcess cl \
threads=10 \
processes=64 \
python-path=/usr/local/lib/python3.8/site-packages/ \
lang='en_US.UTF-8' \
locale='en_US.UTF-8'
WSGIProcessGroup cl
WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On
当我登录服务器并启动时python
在终端中,这条线工作正常,但是通过 mod_wsgi 运行时失败:
from reporters_db import REPORTERS
该行所做的只是导入一个 json 文件,其中包含一些 utf-8 内容。这是导入背后的代码:
db_root = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(db_root, "data", "reporters.json")) as f:
REPORTERS = json.load(f, object_hook=datetime_parser)
由于上面的 json 调用没有指定编码,因此它使用 ASCII 并失败:
Traceback (most recent call last):
File "/opt/courtlistener/docker/apache/wsgi-configs/python_version_test.py", line 6, in <module>
from reporters_db import REPORTERS
File "/usr/local/lib/python3.8/site-packages/reporters_db/__init__.py", line 22, in <module>
REPORTERS = json.load(f, object_hook=datetime_parser)
File "/usr/local/lib/python3.8/json/__init__.py", line 293, in load
return loads(fp.read(),
File "/usr/local/lib/python3.8/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 441720: ordinal not in range(128)
我怎样才能告诉它(以及我的代码库的其余部分)像理智的成年人一样使用 utf-8 ?
Edit 1
也许值得一提的是我正在使用以下命令运行 apache:
exec apache2ctl -D FOREGROUND "$@"
我以为这会来源/etc/apache2/envvars
文件,所以我将以下内容附加到该文件中:
export LANG="en_US.UTF-8"
我尝试将启动命令调整为:
LANG="en_US.UTF-8" exec apache2ctl -D FOREGROUND "$@"
我满怀希望,但没有。还是没有进展。