我有时会在我的网络服务器上遇到此错误。
Traceback (most recent call last):
File "/bin/user_wsgi_wrapper.py", line 130, in __call__
self.error_log_file.logger.exception("Error running WSGI application")
File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
self.error(msg, *args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
self.threadName = threading.current_thread().name
File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
return _active[_get_ident()]
File "/bin/user_wsgi_wrapper.py", line 122, in __call__
app_iterator = self.app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1690, in wsgi_app
return response(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 1082, in __call__
app_iter, status, headers = self.get_wsgi_response(environ)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 1072, in get_wsgi_response
return app_iter, self.status, headers.to_list()
File "/usr/local/lib/python2.7/dist-packages/werkzeug/datastructures.py", line 1141, in to_list
for k, v in self]
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u03c7' in position 66: ordinal not in range(256)
问题是我不知道代码中哪里会发生这种情况,所以我不知道应该将 .encode('utf-8', 'ignore') 放在哪里。
最佳答案
您似乎正在尝试设置一个包含不在 ISO-8859-1 字符集中的字符的 header 。目前,HTTP header 只能包含来自该编解码器的字符。 Werkzeug 的更新版本需要 Latin-1 编解码器,而不是更严格地遵循 WSGI 规范。
See this Flask issue for a message about this from the maintainer.它提到在RFC 5987 中描述了使用范围外字符的正确方法。 ,可能会在未来实现。
关于python - 统一码编码错误 : 'latin-1' codec can't encode character u'\u03c7' in position 66: ordinal not in range(256),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29164105/