简体   繁体   English

python3使用apache WSGI默认编码UnicodeDecodeError ascii

[英]python3 default encoding UnicodeDecodeError ascii using apache WSGI

import locale
prefered_encoding = locale.getpreferredencoding()
prefered_encoding 'ANSI_X3.4-1968'

I'm using a framework called inginious and it's using web.py to render its template. 我正在使用一个名为inginious的框架,它正在使用web.py来呈现其模板。

web.template.render(os.path.join(root_path, dir_path),
                                   globals=self._template_globals,
                                   base=layout_path)

The rendering works on my localhost but not on my staging server . 渲染在我的本地主机上运行,不在我的临时服务器上运行

They both run python3. 他们都运行python3。 I see that web.py enforces utf-8 on 我看到web.py强制执行utf-8

the encoding in Python2 only (that's out of my hands) 仅在Python2中编码(这不在我的手中)

def __str__(self):
    self._prepare_body()
    if PY2:
        return self["__body__"].encode('utf-8')
    else:
        return self["__body__"]

here is the stack trace 这是堆栈跟踪

t = self._template(name),
File "/lib/python3.5/site-packages/web/template.py", line 1028, in _template,
self._cache[name] = self._load_template(name),
File "/lib/python3.5/site-packages/web/template.py", line 1016, in _load_template
return Template(open(path).read(), filename=path, **self._keywords)
File "/lib64/python3.5/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 83: ordinal not in range(128),

My html do include hebew chars, small example 我的html包括hebew chars,小例子

<div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title feedback-modal-title">
                            חישוב האיברים הראשונים בסדרה של איבר ראשון חיובי ויחס שלילי:
                            <span class="red-text">אי הצלחה</span>

and I open it like so : 我这样打开它:

open('/path/to/feedback.html').read()

and the line where the encoding fails is where the Hebrew chars are. 并且编码失败的行是希伯来字符所在的行。

I tried setting some environment variables in ~/.bashrc : 我尝试在~/.bashrc设置一些环境变量:

export PYTHONIOENCODING=utf8
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8

under the user centos 在用户centos

The ingenious framework is installed as a pip under python3.5 site-packages. 巧妙的框架在python3.5 site-packages下作为一个pip安装。 and it served by an apache server under the user apache 它由用户apache下的apache服务器提供服务

Tried setting the environment variables in the code (during the init of the app) so that the apache WSGI will be aware of them 尝试在代码中设置环境变量(在应用程序初始化期间),以便apache WSGI知道它们

import os 
os.environ['LC_ALL'] = 'en_US.UTF-8'
os.environ['LANG'] = 'en_US.UTF-8'
os.environ['LANGUAGE'] = 'en_US.UTF-8'

I have edited the /etc/httpd/conf/httpd.conf using the setenv method: 我使用setenv方法编辑了/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

and restarted using sudo service httpd restart and still no luck. 并重新启动使用sudo service httpd restart仍然没有运气。

My question is, what is the best practice to solve this. 我的问题是,解决这个问题的最佳做法是什么。 I understand there are hacks for this, but I want to understand what is the underline cause as well as how to solve it. 我知道有这方面的黑客,但我想了解什么是下划线原因以及如何解决它。

Thanks! 谢谢!

finally found the answer when reading the file changed from 最后在读取文件时找到了答案

open('/path/to/feedback.html').read()

to

import codecs
with codecs.open(file_path,'r',encoding='utf8') as f:
     text = f.read()

if anyone has a more general approach that will work, I'll accept his answer 如果有人有更通用的方法可行,我会接受他的回答

A Python 2+3 solution would be: Python 2 + 3解决方案将是:

import io

with io.open(file_path, mode='r', encoding='utf8') as f:
     text = f.read()

See the documentation of io.open . 请参阅io.open的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM