DjangoUnicodeDecodeError 编解码器无法在位置解码字节 0xdd
我在 Centos 5.5 的专用服务器上有一个在 Apache 中运行的 Django Web 应用程序,其中包含 mod_wsgi。
但是,有时(一天六七次)它开始为某些随机页面提供 500 错误。但如果我刷新页面两三次,页面就会恢复正常。 但是一旦它开始给出 500 错误,我网站上的所有其他页面都会给出该错误。
我重新启动apache后,它在5或6小时内恢复正常,但它永远不会结束给出该错误。
我将完整的错误日志粘贴在下面,但实际上它是这样说的 DjangoUnicodeDecodeError: 'utf8' 编解码器无法解码位置 330 中的字节 0xdd:无效的连续字节。
什么可能导致这种错误以及如何检测它? 我可以尽快提供任何其他信息。
PS:我在本地 PC(Apache-mod_wsgi)Win7
上进行了相同的设置,但从未遇到过此错误。
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] mod_wsgi (pid=30331): Exception occurred processing WSGI script '/var/www/html/MY_SITE/django.wsgi'., referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] Traceback (most recent call last):, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 273, in __call__, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] response = self.get_response(request), referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/core/handlers/base.py", line 169, in get_response, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] response = self.handle_uncaught_exception(request, resolver, sys.exc_info()), referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] return debug.technical_500_response(request, *exc_info), referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/views/debug.py", line 59, in technical_500_response, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] html = reporter.get_traceback_html(), referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/views/debug.py", line 117, in get_traceback_html, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] frame['vars'] = [(k, force_escape(pprint(v))) for k, v in frame['vars']], referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/template/defaultfilters.py", line 34, in _dec, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] args[0] = force_unicode(args[0]), referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/utils/encoding.py", line 93, in force_unicode, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] raise DjangoUnicodeDecodeError(s, *e.args), referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xdd in position 330: invalid continuation byte. You passed in "<WSGIRequest\\nGET:<QueryDict: {}>,\\nPOST:<QueryDict: {}>,\\nCOOKIES:{},\\nMETA:{'CSRF_COOKIE': '041ed0a93c4b355d4861a0662d49fcb4',\\n 'DOCUMENT_ROOT': '/var/www/html/MY_SITE',\\n 'GATEWAY_INTERFACE': 'CGI/1.1',\\n 'HTTP_ACCEPT': 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',\\n 'HTTP_ACCEPT_ENCOD\\xddNG': 'gzip, deflate',\\n 'HTTP_ACCEPT_LANGUAGE': 'en-us',\\n 'HTTP_CACHE_CONTROL': 'max-age=0',\\n 'HTTP_CONNECT\\xddON': 'keep-alive',\\n 'HTTP_COOK\\xddE': 'csrftoken=10bc570d4ef77b17ce580106dafa9fb6; sessionid=60fb98634573194f7f5e18ef6014f59b',\\n 'HTTP_HOST': 'www.MY_SITE.com',\\n 'HTTP_REFERER': 'http://www.MY_SITE.com/',\\n 'HTTP_USER_AGENT': 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10',\\n 'PATH_INFO': u'/main/faq/',\\n 'PATH_TRANSLATED': '/var/www/html/MY_SITE/django.wsgi/main/faq/',\\n 'QUERY_STRING': '',\\n 'REMOTE_ADDR': '46.104.250.30',\\n 'REMOTE_PORT': '49643',\\n 'REQUEST_METHOD': 'GET',\\n 'REQUEST_URI': '/main/faq/',\\n 'SCRIPT_FILENAME': '/var/www/html/MY_SITE/django.wsgi',\\n 'SCRIPT_NAME': u'',\\n 'SERVER_ADDR': '93.94.251.82',\\n 'SERVER_ADMIN': 'bilgi@MY_SITE.com',\\n 'SERVER_NAME': 'www.MY_SITE.com',\\n 'SERVER_PORT': '80',\\n 'SERVER_PROTOCOL': 'HTTP/1.1',\\n 'SERVER_SIGNATURE': '<address>Apache/2.2.3 (CentOS) Server at www.MY_SITE.com Port 80</address>\\\\n',\\n 'SERVER_SOFTWARE': 'Apache/2.2.3 (CentOS)',\\n 'mod_wsgi.application_group': 'MY_SITE.com|',\\n 'mod_wsgi.callable_object': 'application',\\n 'mod_wsgi.handler_script': '',\\n 'mod_wsgi.input_chunked': '0',\\n 'mod_wsgi.listener_host': '',\\n 'mod_wsgi.listener_port': '80',\\n 'mod_wsgi.process_group': '',\\n 'mod_wsgi.request_handler': 'wsgi-script',\\n 'mod_wsgi.script_reloading': '1',\\n 'mod_wsgi.version': (3, 3),\\n 'wsgi.errors': <mod_wsgi.Log object at 0x2b7d75ddbfb0>,\\n 'wsgi.file_wrapper': <built-in method file_wrapper of mod_wsgi.Adapter object at 0x2b7d75f12a80>,\\n 'wsgi.input': <mod_wsgi.Input object at 0x2b7d75fa0a30>,\\n 'wsgi.multiprocess': True,\\n 'wsgi.multithread': False,\\n 'wsgi.run_once': False,\\n 'wsgi.url_scheme': 'http',\\n 'wsgi.version': (1, 1)}>" (<type 'str'>), referer: http://www.MY_SITE.com/
该网站可以在我本地计算机上的 Apache 和 Django 内置开发服务器(Win7、Apache、mod_wsgi)中无缝运行
I have a Django Web Application running in Apache with mod_wsgi in my dedicated server with Centos 5.5.
However, sometimes (six or seven times in a day) it starts to give 500 Error for some random pages. But if I refresh the page two or three times, the page turns to normal.
But once it starts to give 500 error, every other page in my web site gives that error.
After I restarted the apache, it turns to normal for 5 or 6 hours but it never ends to give that error.
I paste the full error log below but indeed it says thatDjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xdd in position 330: invalid continuation byte.
What may cause this kind of error and how can I detect it ?
I may provide any additional information asap.
PS: I have same setup on my local PC(Apache-mod_wsgi) with Win7
and I have never got this error.
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] mod_wsgi (pid=30331): Exception occurred processing WSGI script '/var/www/html/MY_SITE/django.wsgi'., referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] Traceback (most recent call last):, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 273, in __call__, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] response = self.get_response(request), referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/core/handlers/base.py", line 169, in get_response, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] response = self.handle_uncaught_exception(request, resolver, sys.exc_info()), referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] return debug.technical_500_response(request, *exc_info), referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/views/debug.py", line 59, in technical_500_response, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] html = reporter.get_traceback_html(), referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/views/debug.py", line 117, in get_traceback_html, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] frame['vars'] = [(k, force_escape(pprint(v))) for k, v in frame['vars']], referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/template/defaultfilters.py", line 34, in _dec, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] args[0] = force_unicode(args[0]), referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] File "/opt/python2.7.1/lib/python2.7/site-packages/django/utils/encoding.py", line 93, in force_unicode, referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] raise DjangoUnicodeDecodeError(s, *e.args), referer: http://www.MY_SITE.com/
[Wed Sep 28 12:03:53 2011] [error] [client 46.104.250.30] DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xdd in position 330: invalid continuation byte. You passed in "<WSGIRequest\\nGET:<QueryDict: {}>,\\nPOST:<QueryDict: {}>,\\nCOOKIES:{},\\nMETA:{'CSRF_COOKIE': '041ed0a93c4b355d4861a0662d49fcb4',\\n 'DOCUMENT_ROOT': '/var/www/html/MY_SITE',\\n 'GATEWAY_INTERFACE': 'CGI/1.1',\\n 'HTTP_ACCEPT': 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',\\n 'HTTP_ACCEPT_ENCOD\\xddNG': 'gzip, deflate',\\n 'HTTP_ACCEPT_LANGUAGE': 'en-us',\\n 'HTTP_CACHE_CONTROL': 'max-age=0',\\n 'HTTP_CONNECT\\xddON': 'keep-alive',\\n 'HTTP_COOK\\xddE': 'csrftoken=10bc570d4ef77b17ce580106dafa9fb6; sessionid=60fb98634573194f7f5e18ef6014f59b',\\n 'HTTP_HOST': 'www.MY_SITE.com',\\n 'HTTP_REFERER': 'http://www.MY_SITE.com/',\\n 'HTTP_USER_AGENT': 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10',\\n 'PATH_INFO': u'/main/faq/',\\n 'PATH_TRANSLATED': '/var/www/html/MY_SITE/django.wsgi/main/faq/',\\n 'QUERY_STRING': '',\\n 'REMOTE_ADDR': '46.104.250.30',\\n 'REMOTE_PORT': '49643',\\n 'REQUEST_METHOD': 'GET',\\n 'REQUEST_URI': '/main/faq/',\\n 'SCRIPT_FILENAME': '/var/www/html/MY_SITE/django.wsgi',\\n 'SCRIPT_NAME': u'',\\n 'SERVER_ADDR': '93.94.251.82',\\n 'SERVER_ADMIN': 'bilgi@MY_SITE.com',\\n 'SERVER_NAME': 'www.MY_SITE.com',\\n 'SERVER_PORT': '80',\\n 'SERVER_PROTOCOL': 'HTTP/1.1',\\n 'SERVER_SIGNATURE': '<address>Apache/2.2.3 (CentOS) Server at www.MY_SITE.com Port 80</address>\\\\n',\\n 'SERVER_SOFTWARE': 'Apache/2.2.3 (CentOS)',\\n 'mod_wsgi.application_group': 'MY_SITE.com|',\\n 'mod_wsgi.callable_object': 'application',\\n 'mod_wsgi.handler_script': '',\\n 'mod_wsgi.input_chunked': '0',\\n 'mod_wsgi.listener_host': '',\\n 'mod_wsgi.listener_port': '80',\\n 'mod_wsgi.process_group': '',\\n 'mod_wsgi.request_handler': 'wsgi-script',\\n 'mod_wsgi.script_reloading': '1',\\n 'mod_wsgi.version': (3, 3),\\n 'wsgi.errors': <mod_wsgi.Log object at 0x2b7d75ddbfb0>,\\n 'wsgi.file_wrapper': <built-in method file_wrapper of mod_wsgi.Adapter object at 0x2b7d75f12a80>,\\n 'wsgi.input': <mod_wsgi.Input object at 0x2b7d75fa0a30>,\\n 'wsgi.multiprocess': True,\\n 'wsgi.multithread': False,\\n 'wsgi.run_once': False,\\n 'wsgi.url_scheme': 'http',\\n 'wsgi.version': (1, 1)}>" (<type 'str'>), referer: http://www.MY_SITE.com/
The web site works seamlessly both in Apache and Django's built-in developer server at my local computer(Win7,Apache,mod_wsgi)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在此请求中,客户端发送一个
,其中包含客户端标头'HTTP_ACCEPT_ENCOD\\xddNG': 'gzip, deflate'
。如果
的实际编码应该是 UTF-8,则服务器错误是合法的(除非这实际上不是客户端发送的内容)。在 UTF-8 字节值0xdd
中只能用作两字节字符编码的第一个,在这种情况下,下一个字节必须具有前导位10
。但这里0xdd
后面的字节有一个前导0
位,因此这是使用 UTF-8 的解码错误。如果
的实际编码是其他内容,则服务器错误可能不合法,因为客户端标头可能被解释为'HTTP_ACCEPT_ENCODÝNG': 'gzip, deflate'< /code> (在 ISO-8859-1 的情况下)并被忽略。
尝试识别发送这些请求的特定客户端。
In this request the client is sending a
<WSGIRequest>
with the client header'HTTP_ACCEPT_ENCOD\\xddNG': 'gzip, deflate'
in it.If the actual encoding of the
<WSGIRequest>
is supposed to be UTF-8, then the server error is legitimate (unless this is not really what the client sent). In UTF-8 byte value0xdd
can only be used as the first of a two-byte character coding, in which case the next byte must have leading bits10
. But here the byte following0xdd
has a leading0
bit, so that's a decode error using UTF-8.If the actual encoding of the
<WSGIRequest>
is something else, then the server error may not be legitimate because the client header could be interpreted as'HTTP_ACCEPT_ENCODÝNG': 'gzip, deflate'
(in the case of ISO-8859-1) and ignored.Try to identify the specific client that is sending in these requests.
你有两个问题。
(1) 您的服务器和该用户之间的某些内容正在破坏用户发送给您的标头,将
HTTP_CONNECTION
转换为HTTP_CONNECTÝON
。这种事情通常是由更古老、更愚蠢的网络代理来完成的,有时被误导的移动互联网提供商也会使用这种代理。在这种情况下,他们甚至会破坏您的
HTTP_COOKIE
,这肯定会使您的应用程序无法正常运行,即使您解决了其他问题:(2) 这是 Django 中的一个错误,它会导致尝试读取内部包含非 ASCII/非 UTF-8 字节序列的标头名称时出现异常。 HTTP 显式定义标头名称以 ISO-8859-1 表示,因此 Django 应该使用此编码将标头名称转换为 Unicode 而不是 UTF-8。所有字节序列在 ISO-8859-1 中都是有效的,因此永远不应该出现 UnicodeDecodeError。
实际上,不存在使用非 ASCII 名称的标头,并且浏览器对非 ASCII 标头值的处理是一个坎坷且不一致的过程。但是,Django 仍然应该允许并忽略伪造的标头。
You have two problems.
(1) Something between your server and this user is mangling the header the user's sending you, turning
HTTP_CONNECTION
intoHTTP_CONNECTÝON
. This kind of thing is usually done by the more ancient and brain-dead kinds of web proxy sometimes used by misguided mobile internet providers.In this case they're even screwing with your
HTTP_COOKIE
, which is sure to make your app non-functional anyway, even if you fix the other problem:(2) It's a bug in Django that it gets an exception trying to read a header name with a non-ASCII/non-UTF-8 byte sequence inside. HTTP explicitly defines header names as being represented in ISO-8859-1 so Django should be using this encoding to convert the header names to Unicode and not UTF-8. All byte sequences are valid in ISO-8859-1 so there should never be a UnicodeDecodeError.
In practice there are no headers that ever use non-ASCII names, and browser handling of non-ASCII header values is a bumpy and inconsistent ride. But still, Django should allow, and ignore, the bogus headers.