CommonService.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: langer
  7. @software: PyCharm
  8. @DATE: 2019/9/4 17:18
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: CommonService.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. from random import Random # 用于生成随机码
  15. import hashlib
  16. from urllib.request import urlopen
  17. # 通过url获取文件md5
  18. class CommonService:
  19. @staticmethod
  20. def get_remote_md5_sum(url, max_file_size=100 * 1024 * 1024):
  21. remote = urlopen(url)
  22. hash = hashlib.md5()
  23. total_read = 0
  24. while True:
  25. data = remote.read(4096)
  26. total_read += 4096
  27. if not data or total_read > max_file_size:
  28. break
  29. hash.update(data)
  30. return hash.hexdigest()
  31. @staticmethod
  32. def RandomStr(randomlength=8, number=False):
  33. str = ''
  34. if number == False:
  35. characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \
  36. 'tUuVvWwXxYyZz0123456789'
  37. else:
  38. characterSet = '0123456789'
  39. length = len(characterSet) - 1
  40. random = Random()
  41. for index in range(randomlength):
  42. str += characterSet[random.randint(0, length)]
  43. return str
  44. # print(CommonService.get_remote_md5_sum(url='http://www.dvema.com/web/asdf'))