redisClient.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. """
  3. -----------------------------------------------------
  4. File Name: redisClient.py
  5. Description : 封装Redis相关操作
  6. Author : JHao
  7. date: 2019/8/9
  8. ------------------------------------------------------
  9. Change Activity:
  10. 2019/08/09: 封装Redis相关操作
  11. 2020/06/23: 优化pop方法, 改用hscan命令
  12. ------------------------------------------------------
  13. """
  14. __author__ = 'JHao'
  15. from redis.connection import BlockingConnectionPool
  16. from redis import Redis
  17. class RedisClient(object):
  18. """
  19. Redis client
  20. Redis中代理存放的结构为hash:
  21. 原始代理存放在name为raw_proxy的hash中, key为ip:port, value为代理属性的字典;
  22. 验证后的代理存放在name为use_proxy的hash中, key为代理的ip:port, value为代理属性的字典;
  23. """
  24. def __init__(self, **kwargs):
  25. """
  26. init
  27. :param host: host
  28. :param port: port
  29. :param password: password
  30. :param db: db
  31. :return:
  32. """
  33. self.name = ""
  34. kwargs.pop("username")
  35. self.__conn = Redis(connection_pool=BlockingConnectionPool(decode_responses=True, **kwargs))
  36. def get(self, proxy_str):
  37. """
  38. 从hash中获取对应的proxy, 使用前需要调用changeTable()
  39. :param proxy_str: proxy str
  40. :return:
  41. """
  42. data = self.__conn.hget(name=self.name, key=proxy_str)
  43. return data
  44. def put(self, proxy_obj):
  45. """
  46. 将代理放入hash, 使用changeTable指定hash name
  47. :param proxy_obj: Proxy obj
  48. :return:
  49. """
  50. data = self.__conn.hset(self.name, proxy_obj.proxy, proxy_obj.info_json)
  51. return data
  52. def delete(self, proxy_str):
  53. """
  54. 移除指定代理, 使用changeTable指定hash name
  55. :param proxy_str: proxy str
  56. :return:
  57. """
  58. self.__conn.hdel(self.name, proxy_str)
  59. def exists(self, proxy_str):
  60. """
  61. 判断指定代理是否存在, 使用changeTable指定hash name
  62. :param proxy_str: proxy str
  63. :return:
  64. """
  65. return self.__conn.hexists(self.name, proxy_str)
  66. def update(self, proxy_obj):
  67. """
  68. 更新 proxy 属性
  69. :param proxy_obj:
  70. :return:
  71. """
  72. self.__conn.hset(self.name, proxy_obj.proxy, proxy_obj.info_json)
  73. def pop(self):
  74. """
  75. 弹出一个代理
  76. :return: dict {proxy: value}
  77. """
  78. cursor, data_dict = self.__conn.hscan(self.name, count=1)
  79. for key, value in data_dict.items():
  80. self.__conn.hdel(self.name, key)
  81. return value
  82. else:
  83. return False
  84. def getAll(self):
  85. """
  86. 字典形式返回所有代理, 使用changeTable指定hash name
  87. :return:
  88. """
  89. item_dict = self.__conn.hgetall(self.name)
  90. return item_dict
  91. def clear(self):
  92. """
  93. 清空所有代理, 使用changeTable指定hash name
  94. :return:
  95. """
  96. return self.__conn.delete(self.name)
  97. def getCount(self):
  98. """
  99. 返回代理数量
  100. :return:
  101. """
  102. return self.__conn.hlen(self.name)
  103. def changeTable(self, name):
  104. """
  105. 切换操作对象
  106. :param name: raw_proxy/use_proxy
  107. :return:
  108. """
  109. self.name = name