middleware.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. return 0
  32. def _https_statistics_to_close(self, request):
  33. '''
  34. :param request:
  35. :return:
  36. '''
  37. addsCount = len(api_settings.ADDR_URL)
  38. if addsCount > 0:
  39. api_settings.ADDR_URL.pop(0)
  40. def process_request(self, request):
  41. '''
  42. :function description
  43. Request预处理函数: process_request(self, request)
  44. :param request:
  45. :return: 应当返回 None 或 HttpResponse 对象
  46. 如果返回 None ,Django 将继续处理这个 request,执行后续的中间件,
  47. 然后调用相应的 view。
  48. 如果返回 HttpResponse 对象,Django 将不再执行任何其它的中间件
  49. (无视其种类)以及相应的view。 Django将立即返回该 HttpResponse。
  50. '''
  51. if request.path != '/favicon.ico':
  52. print('process_request', request)
  53. result = self._https_statistics_to_reverse(request)
  54. if result == -1:
  55. return ResponseJSON(910)
  56. return None
  57. def process_view(self, request, callback, callback_args, callback_kwargs):
  58. if request.path != '/favicon.ico':
  59. print('process_view', request)
  60. return None
  61. def process_response(self, request, response):
  62. '''
  63. :function description
  64. Response后处理函数: process_response(self, request, response)
  65. 这个方法的调用时机在 Django 执行 view 函数并生成 response 之后。
  66. :param request: request 对象
  67. :param response: 从 view 中返回的 response 对象
  68. :return: 必须返回 HttpResponse 对象. 这个 response 对象可以是传入函数的那一个原始对象(通常已被修改),也可以是全新生成的。
  69. 该处理器能修改 response 的内容;一个常见的用途是内容压缩,如 gzip 所请求的 HTML 页面。
  70. '''
  71. self._https_statistics_to_close(request)
  72. ########记录访问日志
  73. if request.path !='/favicon.ico':
  74. print('process_response', request, response)
  75. try:
  76. # mysql
  77. MiscellService.add_access_log(request=request, status_code=response.status_code)
  78. # mongodb版
  79. # MiscellService.access_log(request=request, response=response, type=0)
  80. except Exception as e:
  81. print(repr(e))
  82. return response