带有重复 AJAX 调用的 Django 关闭套接字错误 [WinError 10053] 已建立的连接被主机中的软件中止

Posted

技术标签:

【中文标题】带有重复 AJAX 调用的 Django 关闭套接字错误 [WinError 10053] 已建立的连接被主机中的软件中止【英文标题】:Django Closed Socket Error with Repeated AJAX Calls [WinError 10053] An established connection was aborted by the software in your host machine 【发布时间】:2018-01-31 04:22:36 【问题描述】:

我正在尝试通过我的 Django 1.11.4 项目中的 jquery 插件 Select2 实现实时搜索。运行 Python 3.6。当我在文本框中输入时,服务器似乎无法处理请求的数量并关闭,随后出现一系列错误。 我花了几个小时关注SOSO(more)、Google、etc 上的几个线程,但没有一个有效的解决方案。我以为是 python 版本问题,但我已经从 2.7 升级到 3.6 并且仍然有它。任何帮助都会非常感谢!

这是由 ajax 调用调用的视图:

  def directory_search(request):
      # Query text from search bar
      query = request.GET.get('q', None)
      if query:

          # Searches the DB for matching value
          customers = Customer.objects.filter(
              Q(name__icontains= query)|
              Q(contract_account__icontains= query)|
              Q(address__icontains= query)|
              Q(address__icontains= query)|
              Q(email__icontains= query)
              ).distinct()
          # Turns queryset into dict
          customers = customers.values("contract_account","name")
          # Turns dict into list
          customers_list = list(customers)

          return JsonResponse(customers_list, safe=False)
      else:
          return JsonResponse(data='success': False,'errors': 'No mathing items found')

这是 Select2 插件的 js/ajax 调用:

  $(".customer-search-bar").select2(
    ajax:
      dataType: 'json',
      type: 'GET',
      url:"% url 'portal:directory_search' %",
      data: function (params) 
          var queryParameters = 
              q: params.term
          
          return queryParameters;
      ,
      processResults: function (data) 
          return 
              results: $.map(data, function (item) 
                  return 
                      text: item.name,
                      id: item.contract_account
                  
              )
          ;
      
    ,
    escapeMarkup: function (markup)  return markup; ,
    minimumInputLength: 1,
    templateResult: formatRepo,
    templateSelection: formatRepoSelection,
    language:  errorLoading:function() return "Searching..." 
  );

错误:(我只是将它们分开以使其更清晰,但它们按呈现的顺序发生)

1.

Traceback (most recent call last):
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 332, in send_headers
    self.send_preamble()
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 255, in send_preamble
    ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 453, in _write
    result = self.stdout.write(data)
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 775, in write
    self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

2.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 141, in run
    self.handle_error()
  File "C:\Users\leep\PythonStuff\virtual_environments\rpp_3.6\lib\site-packages\django\core\servers\basehttp.py", line 88, in handle_error
    super(ServerHandler, self).handle_error()
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 368, in handle_error
    self.finish_response()
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 331, in send_headers
    if not self.origin_server or self.client_is_modern():
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 344, in client_is_modern
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable

3.

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 639, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 361, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 696, in __init__
    self.handle()
  File "C:\Users\leep\PythonStuff\virtual_environments\rpp_3.6\lib\site-packages\django\core\servers\basehttp.py", line 155, in handle
    handler.run(self.server.get_app())
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 144, in run
    self.close()
  File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\simple_server.py", line 35, in close
    self.status.split(' ',1)[0], self.bytes_sent
  AttributeError: 'NoneType' object has no attribute 'split'

【问题讨论】:

【参考方案1】:

它似乎是 python 2.7 已知的错误,也许它仍然存在于 python 3.6:

https://bugs.python.org/issue14574

我在 django 中遇到了类似的问题,也在运行 python 3.6 TypeError: 'NoneType' object is not subscriptable followed by AttributeError: 'NoneType' object has no attribute 'split'

编辑

这似乎是 chrome 上的错误。我没有想到,因为我使用的是 Opera,但 Opera 也使用了 Chromium。在 Internet Explorer 中没有显示错误。

https://code.djangoproject.com/ticket/21227#no1

这是错误跟踪器

【讨论】:

以上是关于带有重复 AJAX 调用的 Django 关闭套接字错误 [WinError 10053] 已建立的连接被主机中的软件中止的主要内容,如果未能解决你的问题,请参考以下文章

Django如何通过单击带有变量的提交按钮从ajax调用views.py中的函数

Django + ajax(jquery):http错误代码403(禁止)[关闭]

带有 jquery 的 Django 模板:现有页面上的 Ajax 更新

angularjs中重复的ajax调用

在 django 应用程序中加载的 Ajax 调用时间

我无法从 JQuery ajax 调用中加载部分 Django 模板