middleware.py 3.8 KB

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