middleware.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from Ansjer import settings as api_settings
  4. from Service.MiscellService import MiscellService
  5. from Service.ResponseService import ResponseJSON
  6. from django.utils.deprecation import MiddlewareMixin
  7. '''
  8. try:
  9. from django.utils.deprecation import MiddlewareMixin
  10. except ImportError: # pragma: no cover
  11. # Not required for Django <= 1.9, see:
  12. # https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware
  13. MiddlewareMixin = object # pragma: no cover
  14. '''
  15. class StatisticsUrlMiddleware(MiddlewareMixin):
  16. def _https_statistics_to_reverse(self, request):
  17. '''
  18. :param request:
  19. :return:
  20. '''
  21. # 限制同时下载量
  22. path = request.path
  23. index = path.find('/OTA/downloads')
  24. if index != -1:
  25. addsCount = len(api_settings.ADDR_URL)
  26. if addsCount > 1000:
  27. return -1
  28. else:
  29. adds = request.META.get('REMOTE_ADDR', None)
  30. api_settings.ADDR_URL.append(adds)
  31. MiscellService.add_ota_download_log(request=request)
  32. return 0
  33. def _https_statistics_to_close(self, request):
  34. '''
  35. :param request:
  36. :return:
  37. '''
  38. addsCount = len(api_settings.ADDR_URL)
  39. if addsCount > 0:
  40. api_settings.ADDR_URL.pop(0)
  41. def process_request(self, request):
  42. '''
  43. :function description
  44. Request预处理函数: process_request(self, request)
  45. :param request:
  46. :return: 应当返回 None 或 HttpResponse 对象
  47. 如果返回 None ,Django 将继续处理这个 request,执行后续的中间件,
  48. 然后调用相应的 view。
  49. 如果返回 HttpResponse 对象,Django 将不再执行任何其它的中间件
  50. (无视其种类)以及相应的view。 Django将立即返回该 HttpResponse。
  51. '''
  52. if request.path != '/favicon.ico':
  53. print('process_request', request)
  54. result = self._https_statistics_to_reverse(request)
  55. if result == -1:
  56. return ResponseJSON(910)
  57. return None
  58. def process_view(self, request, callback, callback_args, callback_kwargs):
  59. if request.path != '/favicon.ico':
  60. print('process_view', request)
  61. return None
  62. def process_response(self, request, response):
  63. '''
  64. :function description
  65. Response后处理函数: process_response(self, request, response)
  66. 这个方法的调用时机在 Django 执行 view 函数并生成 response 之后。
  67. :param request: request 对象
  68. :param response: 从 view 中返回的 response 对象
  69. :return: 必须返回 HttpResponse 对象. 这个 response 对象可以是传入函数的那一个原始对象(通常已被修改),也可以是全新生成的。
  70. 该处理器能修改 response 的内容;一个常见的用途是内容压缩,如 gzip 所请求的 HTML 页面。
  71. '''
  72. self._https_statistics_to_close(request)
  73. ########记录访问日志
  74. if request.path !='/favicon.ico':
  75. print('process_response', request, response)
  76. try:
  77. # mysql
  78. MiscellService.add_access_log(request=request, status_code=response.status_code)
  79. # mongodb版
  80. # MiscellService.access_log(request=request, response=response, type=0)
  81. except Exception as e:
  82. print(repr(e))
  83. return response