django UnreadablePostError: request data read error

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

I'm working on django project and I got this error email.

Stack trace

File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/core/handlers/wsgi.py", line 180, in _get_post     self._load_post_and_files()  File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/http/__init__.py", line 379, in _load_post_and_files     self._post, self._files = QueryDict(self.body, encoding=self._encoding), MultiValueDict()  File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/http/__init__.py", line 335, in body     self._body = self.read()  File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/http/__init__.py", line 391, in read     return self._stream.read(*args, **kwargs)  File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/core/handlers/wsgi.py", line 98, in read     result = self.buffer + self._read_limited()  File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/core/handlers/wsgi.py", line 92, in _read_limited     result = self.stream.read(size)  UnreadablePostError: request data read error 

Why this error is happening ?
How to solve?

回答1:

Why this error is happening ?

because the server is recieving a malformed request, which can happen for many reasons. someone might've canceled loading the page, someone might have a crappy internet connection that cut out, cosmic rays could have flipped a bit.

it's not something you really need to worry about until it starts happening very frequently. you might want to make a note when these errors happen and see if it's consequently on the same page or not.

How to solve?

you can't. not at this point at least. gather some more data on when this bug occurs exactly. see if you can find a way to trigger it manually.



回答2:

With an application at scale, you'll always get the occasional cancelled request. If you're receiving 500 emails they can be quite tedious.

I wouldn't advise completely ignoring them. If UnreadablePostErrors are pouring in, then something's wrong e.g. longer response times causes users to cancel requests.

My solution was to ignore 1/20 UnreadablePostErrors. That way, if something goes wrong i'll still be informed, but i'll be pestered 20x less. It's a bit dirty, but it worked for me.

import logging import random from django.http import UnreadablePostError  class ReduceUnreadablePostErrors(logging.Filter):     def filter(self, record):         if record.exc_info:             exc_value = record.exc_info[1]             if isinstance(exc_value, UnreadablePostError):                 return random.randint(1,20) % 20==0         return True 

settings.py:

'filters': {    'reduce_unreadable_post_errors' : {         '()' : 'path.to.your.ReduceUnreadablePostErrors'     },      ...  'handlers': {     'mail_admins': {         'level': 'ERROR',         'filters': ['require_debug_false','reduce_unreadable_post_errors'],         'class': 'common.utils.log.AdminEmailHandlerWithEmail'      },       ... 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!