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

时间:2017-08-22 19:03:26

标签: python ajax django jquery-select2 django-wsgi

我试图通过我的Django 1.11.4项目中的jquery插件Select2实现实时搜索。运行Python 3.6。当我输入文本框时,服务器似乎无法处理请求数量并关闭,然后是这些系列错误。 我已经在SO SO(more)Googleetc上花了几个小时跟踪了几个帖子,但没有一个有可行的解决方案。我认为这是一个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 个答案:

答案 0 :(得分:0)

它似乎是一个已知的python 2.7 bug,也许它仍然存在于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也使用了铬。在Internet Explorer中,错误没有显示。

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

这是错误跟踪器