【问题标题】:How to disable NoReverseMatch error email on production server?如何在生产服务器上禁用 NoReverseMatch 错误电子邮件?
【发布时间】:2012-01-12 06:03:15
【问题描述】:

今天早上,我的收件箱中有大约 100 封电子邮件告诉我各种页面不存在(我怀疑某些机器人正在寻找安全漏洞)。错误是:

NoReverseMatch: Reverse for 'view_profile' with arguments '('',)' and keyword arguments '{}' not found.

但是,当我转到生产服务器上的指定 URL 时,我得到了 404 页面,正如我所期望的那样,并且没有发送任何电子邮件。那么这些机器人为什么/如何产生(我认为是)内部服务器错误?我该如何预防?

我的view_profile urlpattern 定义如下:

url(r'^(?P<id>\d+)$', name="view_profile")

这是一个子模式

RegexURLResolver(r'^contacts/', 'profiles.views.user_profile'),

它的定义与我的任何其他 URL 模式完全一样,并且需要将 ID 传递给它......所以我不知道它是如何在没有参数的情况下被击中的。

完整的跟踪:

Traceback (most recent call last):

 File "/home/me/my/website/lib/python2.7/django/core/handlers/base.py", line 150, in get_response
   response = callback(request, **param_dict)

 File "/home/me/my/website/lib/python2.7/django/utils/decorators.py", line 93, in _wrapped_view
   response = view_func(request, *args, **kwargs)

 File "/home/me/my/website/lib/python2.7/django/views/defaults.py", line 19, in page_not_found
   return http.HttpResponseNotFound(t.render(RequestContext(request, {'request_path': request.path})))

 File "/home/me/my/website/lib/python2.7/django/template/base.py", line 123, in render
   return self._render(context)

 File "/home/me/my/website/lib/python2.7/django/template/base.py", line 117, in _render
   return self.nodelist.render(context)

 File "/home/me/my/website/lib/python2.7/django/template/base.py", line 744, in render
   bits.append(self.render_node(node, context))

 File "/home/me/my/website/lib/python2.7/django/template/base.py", line 757, in render_node
   return node.render(context)

 File "/home/me/my/website/lib/python2.7/django/template/loader_tags.py", line 127, in render
   return compiled_parent._render(context)

 File "/home/me/my/website/lib/python2.7/django/template/base.py", line 117, in _render
   return self.nodelist.render(context)

 File "/home/me/my/website/lib/python2.7/django/template/base.py", line 744, in render
   bits.append(self.render_node(node, context))

 File "/home/me/my/website/lib/python2.7/django/template/base.py", line 757, in render_node
   return node.render(context)

 File "/home/me/my/website/lib/python2.7/django/template/defaulttags.py", line 311, in render
   return self.nodelist_true.render(context)

 File "/home/me/my/website/lib/python2.7/django/template/base.py", line 744, in render
   bits.append(self.render_node(node, context))

 File "/home/me/my/website/lib/python2.7/django/template/base.py", line 757, in render_node
   return node.render(context)

 File "/home/me/my/website/lib/python2.7/django/template/defaulttags.py", line 450, in render
   raise e

NoReverseMatch: Reverse for 'view_profile' with arguments '('',)' and keyword arguments '{}' not found.


<WSGIRequest
GET:<QueryDict: {}>,
POST:<QueryDict: {}>,
COOKIES:{'csrftoken': '89867bc39db49a791bb24dce06e26b6f',
 'sessionid': 'd0db456ae01005678d1538b00ae26dd1'},
META:{'CSRF_COOKIE': '89867bc39db49a791bb24dce06e26b6f',
 'DOCUMENT_ROOT': '/usr/local/apache2/htdocs',
 'GATEWAY_INTERFACE': 'CGI/1.1',
 'HTTP_ACCEPT': '*/*',
 'HTTP_CONNECTION': 'close',
 'HTTP_COOKIE': 'csrftoken=89867bc39db49a791bb24dce06e26b6f; sessionid=d0db456ae01005678d1538b00ae26dd1',
 'HTTP_HOST': 'myproject.net',
 'HTTP_USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6; Zango 10.3.75.0)',
 'HTTP_X_FORWARDED_FOR': '39.47.147.220',
 'HTTP_X_FORWARDED_HOST': 'myproject.net',
 'HTTP_X_FORWARDED_SERVER': 'myproject.net',
 'PATH_INFO': u'/web/',
 'PATH_TRANSLATED': '/home/me/my/website/myproject.wsgi/web/',
 'QUERY_STRING': '',
 'REMOTE_ADDR': '127.0.0.1',
 'REMOTE_PORT': '40577',
 'REQUEST_METHOD': 'GET',
 'REQUEST_URI': '/web/',
 'SCRIPT_FILENAME': '/home/me/my/website/myproject.wsgi',
 'SCRIPT_NAME': u'',
 'SERVER_ADDR': '127.0.0.1',
 'SERVER_ADMIN': '[no address given]',
 'SERVER_NAME': 'myproject.net',
 'SERVER_PORT': '80',
 'SERVER_PROTOCOL': 'HTTP/1.0',
 'SERVER_SIGNATURE': '',
 'SERVER_SOFTWARE': 'Apache/2.2.17 (Unix) mod_wsgi/3.3 Python/2.7.1',
 'mod_wsgi.application_group': 'my.server.com|',
 'mod_wsgi.callable_object': 'application',
 'mod_wsgi.handler_script': '',
 'mod_wsgi.input_chunked': '0',
 'mod_wsgi.listener_host': '',
 'mod_wsgi.listener_port': '54084',
 'mod_wsgi.process_group': '',
 'mod_wsgi.request_handler': 'wsgi-script',
 'mod_wsgi.script_reloading': '1',
 'mod_wsgi.version': (3, 3),
 'wsgi.errors': <mod_wsgi.Log object at 0xae05ef8>,
 'wsgi.file_wrapper': <built-in method file_wrapper of mod_wsgi.Adapter object at 0xb18c1d0>,
 'wsgi.input': <mod_wsgi.Input object at 0xae05e58>,
 'wsgi.multiprocess': False,
 'wsgi.multithread': True,
 'wsgi.run_once': False,
 'wsgi.url_scheme': 'http',
 'wsgi.version': (1, 1)}>

重命名路径以保护无辜者。

【问题讨论】:

    标签: python django http url exception


    【解决方案1】:

    在某个地方(可能在模板中),您使用空字符串而不是 id 解析 view_profile。所以找出它为什么会发生并修复它

    【讨论】:

    • 我刚刚在view_profile 上进行了搜索。所有{% urls %} 都有一个ID。此外,无论如何,它们都被锁定在@login_required 后面,因此它们不应该被访问。就像我说的,如果我访问给定的 URL,我会得到 404 页面........哦......但是使用标准模板......其中有一个用户特定的东西...... .user.pk 可能为空。
    • 是的,你去。在 django docs.djangoproject.com/en/1.3/ref/settings/… 中还有一个有用的选项用于调试此类错误,您将其设置为“EMPTY”并快速找出发生了什么,因为 url 异常将以 EMPTY 作为 arg
    • @DmitrySchvchenko:太好了!不知道那个设置。谢谢!也许我只会在调试模式下将其设为“EMPTY”......那会很好。实际上,这个特定的错误在调试中从来没有发生过,因为你没有得到标准的 404。这可能是我从未注意到它的原因。
    【解决方案2】:

    可能是机器人只是简单地请求所有可映射的 url(就像网站爬虫一样),而不尊重重定向请求或 cookie。

    【讨论】:

      【解决方案3】:

      您可能需要一个 robots.txt 文件来抵御机器人。将此添加到您的主 urls.py 文件中。 DISALLOW_ROBOTS 应在开发环境的 settings.py 中设置为 True。

      if settings.DISALLOW_ROBOTS:
          urlpatterns += patterns('',
              (r'^robots\.txt$', lambda r: HttpResponse("User-agent: *\nDisallow: /", mimetype="text/plain")),
          )
      else:
          urlpatterns += patterns('',
              (r'^robots\.txt$', lambda r: HttpResponse("User-agent: *\nDisallow: ", mimetype="text/plain")),
          )
      

      【讨论】:

      • 我有一个 robots.txt。我有没有提到这很可能是一次黑客攻击?即,我认为他们并不完全尊重 robots.txt。
      猜你喜欢
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 2021-09-21
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多