UnicodeEncodeError:'latin-1'编解码器无法编码位置66中的字符u'\ u03c7':序数不在范围内(256)

时间:2015-03-20 10:15:22

标签: python unicode flask

我有时会在网络服务器上收到此错误。

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','忽略')。

2 个答案:

答案 0 :(得分:6)

您似乎正在尝试设置一个标题,该标题的字符不在ISO-8859-1字符集中。目前,HTTP标头只能包含该编解码器中的字符。更新版本的Werkzeug需要使用Latin-1编解码器来更严格地遵循WSGI规范。

See this Flask issue for a message about this from the maintainer.它提到RFC 5987中描述了使用范围之外的字符的正确方法,可能会在将来实施。

答案 1 :(得分:0)

放一个:

import pdb; pdb.set_trace()

在: 在调用

中输入文件“/bin/user_wsgi_wrapper.py”,第130行

重新启动应用并检查终端中的vars值。 按“n”表示“下一步”。 使用“print var_name”查看变量的值。 在此处阅读更多内容:https://docs.python.org/2/library/pdb.html