You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.3KB

  1. # -*- coding: utf-8 -*-
  2. import time
  3. from json import loads
  4. from threading import Thread
  5. from traceback import format_exc
  6. from loguru import logger
  7. from util.QueUtil import get_block_queue
  8. from util.RequestUtil import HttpRequests
  9. class HttpFeedbackThread(Thread):
  10. __slots__ = '__fb_queue'
  11. def __init__(self, fb_queue):
  12. super().__init__()
  13. self.__fb_queue = fb_queue
  14. def run(self):
  15. logger.info("启动反馈线程")
  16. fb_queue = self.__fb_queue
  17. request = HttpRequests()
  18. try:
  19. headers = {'content-type': "application/json"}
  20. while True:
  21. try:
  22. fb = get_block_queue(fb_queue)
  23. if fb is not None and len(fb) > 0:
  24. if fb[0] == "upload":
  25. upload_req(request, fb[1], headers)
  26. del fb
  27. else:
  28. time.sleep(1)
  29. except Exception:
  30. logger.error("反馈异常:{}", format_exc())
  31. if request:
  32. request.close_session()
  33. request = HttpRequests()
  34. finally:
  35. request.close_session()
  36. logger.info("反馈线程执行完成")
  37. def upload_req(request, upload, headers):
  38. logger.info("开始发送文件上传回调请求, 回调地址: {}", upload['callback_url'])
  39. logger.info("开始发送文件上传回调请求, 回调请求体: {}", upload['data'])
  40. try:
  41. response = request.send_request('POST', upload['callback_url'], data=upload['data'], headers=headers, timeout=3)
  42. if response.status_code != 200:
  43. logger.error("上传回调请求失败! 状态码: {}, {}", response.status_code, response.__dict__)
  44. else:
  45. content = response.content.decode('utf-8')
  46. if content is not None and len(content) > 0:
  47. content = loads(content)
  48. code = content.get('code')
  49. if code is not None and code != 0:
  50. logger.error("上传回调请求失败! 失败描述: {}", content.get('msg'))
  51. except Exception:
  52. logger.error("回调请求失败, 请求体: {}, 回调地址: {}, 异常信息: {}", upload['data'], upload['callback_url'],
  53. format_exc())