common.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """
  2. @author: luojunhui
  3. """
  4. import hashlib
  5. from requests import RequestException
  6. from tenacity import (
  7. stop_after_attempt,
  8. wait_exponential,
  9. retry_if_exception_type,
  10. )
  11. def str_to_md5(strings):
  12. """
  13. 字符串转化为 md5 值
  14. :param strings:
  15. :return:
  16. """
  17. # 将字符串转换为字节
  18. original_bytes = strings.encode("utf-8")
  19. # 创建一个md5 hash对象
  20. md5_hash = hashlib.md5()
  21. # 更新hash对象,传入原始字节
  22. md5_hash.update(original_bytes)
  23. # 获取16进制形式的MD5哈希值
  24. md5_value = md5_hash.hexdigest()
  25. return md5_value
  26. def proxy():
  27. """
  28. 快代理
  29. """
  30. # 隧道域名:端口号
  31. tunnel = "j685.kdltps.com:15818"
  32. # 用户名密码方式
  33. username = "t14070979713487"
  34. password = "hqwanfvy"
  35. proxies = {
  36. "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel},
  37. "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel}
  38. }
  39. return proxies
  40. def request_retry(retry_times, min_retry_delay, max_retry_delay):
  41. """
  42. :param retry_times:
  43. :param min_retry_delay:
  44. :param max_retry_delay:
  45. """
  46. common_retry = dict(
  47. stop=stop_after_attempt(retry_times),
  48. wait=wait_exponential(min=min_retry_delay, max=max_retry_delay),
  49. retry=retry_if_exception_type((RequestException, TimeoutError)),
  50. reraise=True # 重试耗尽后重新抛出异常
  51. )
  52. return common_retry