upload_camera_profile.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. @CreateTime: 2021/8/5 9:54
  5. @Author: lxc
  6. @LastEditTime:
  7. @Desctiption: 上行数据:上传(更新)摄像设备配置文件到云平台
  8. """
  9. import configparser
  10. import time
  11. import os
  12. import requests
  13. from requests_toolbelt.multipart import MultipartEncoder
  14. import LogOut
  15. def post_upload_file():
  16. """
  17. 上传二进制字节流文件
  18. :return: "SUCCESS" or "ERROR"
  19. """
  20. now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
  21. headers = {
  22. "Authorization": token,
  23. "userAccount": '1'
  24. }
  25. multipart_encoder = MultipartEncoder(
  26. fields={
  27. "file": (FILENAME, open(config_path, 'rb'), "multipart/form-data")
  28. }
  29. )
  30. headers['Content-Type'] = multipart_encoder.content_type
  31. requests.urllib3.disable_warnings()
  32. url = "https://management.super-sight.com.cn:8000/system/api/cameraManagement/cameraInfoFileUpload"
  33. ret = requests.post(url=url, headers=headers, data=multipart_encoder, verify=False)
  34. if ret.status_code == 200 and "success" in ret.text:
  35. _logger.info("%s: ret.text=%s" % (now_time, ret.text))
  36. _logger.info("%s: ret.status_code=%s" % (now_time, ret.status_code))
  37. else:
  38. _logger.error("%s: ret.text=%s" % (now_time, ret.text))
  39. _logger.error("%s: ret.status_code=%s" % (now_time, ret.status_code))
  40. def main():
  41. """
  42. 判断文件是否需要更新
  43. :return: 无
  44. """
  45. run_time = 0
  46. file_time = 0
  47. while True:
  48. now_time = time.time()
  49. if now_time - run_time > 60*60:
  50. run_time = now_time
  51. status = os.path.exists(config_path)
  52. if status:
  53. file_new_time = os.path.getmtime(config_path)
  54. if file_time != file_new_time:
  55. file_time = file_new_time
  56. post_upload_file()
  57. else:
  58. _logger.error("未找到目标文件:" + config_path)
  59. if __name__ == '__main__':
  60. _logger = LogOut.Log('uploadCameraProfile')
  61. # 创建读取配置文件对象
  62. config = configparser.ConfigParser()
  63. config.read("config.ini", encoding="utf-8")
  64. # 获取通用配置项
  65. SECTION = "General" # 读取的SECTION标签
  66. token = config.get(SECTION, 'token')
  67. appId = config.get(SECTION, 'appId')
  68. FILEPATH = r"/home/sencott/imageCapture/configration_files"
  69. FILENAME = "云平台导入海上平台摄像配置文件模板.xlsx"
  70. config_path = os.path.join(FILEPATH, FILENAME)
  71. main()