RedisClient.py 3.7 KB

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