SsdbClient.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. """
  4. -------------------------------------------------
  5. File Name: SsdbClient.py
  6. Description : 封装SSDB操作
  7. Author : JHao
  8. date: 2016/12/2
  9. -------------------------------------------------
  10. Change Activity:
  11. 2016/12/2:
  12. 2017/09/22: PY3中 redis-py返回的数据是bytes型
  13. 2017/09/27: 修改pop()方法 返回{proxy:value}字典
  14. -------------------------------------------------
  15. """
  16. __author__ = 'JHao'
  17. from Util import EnvUtil
  18. from redis.connection import BlockingConnectionPool
  19. from redis import Redis
  20. import random
  21. class SsdbClient(object):
  22. """
  23. SSDB client
  24. SSDB中代理存放的容器为hash:
  25. 原始代理存放在name为raw_proxy的hash中,key为代理的ip:port,value为为None,以后扩展可能会加入代理属性;
  26. 验证后的代理存放在name为useful_proxy的hash中,key为代理的ip:port,value为一个计数,初始为1,每校验失败一次减1;
  27. """
  28. # 为了保持DbClient的标准
  29. # 在SsdbClient里面接受username参数, 但不进行使用.
  30. # 因为不能将username通过kwargs传进redis.Redis里面, 会报错:
  31. # TypeError: __init__() got an unexpected keyword argument 'username'
  32. def __init__(self, name, username, **kwargs):
  33. """
  34. init
  35. :param name: hash name
  36. :param host: ssdb host
  37. :param port: ssdb port
  38. :return:
  39. """
  40. self.name = name
  41. self.__conn = Redis(connection_pool=BlockingConnectionPool(**kwargs))
  42. def get(self, proxy):
  43. """
  44. get an item
  45. 从hash中获取对应的proxy, 使用前需要调用changeTable()
  46. :param proxy:
  47. :return:
  48. """
  49. data = self.__conn.hget(name=self.name, key=proxy)
  50. if data:
  51. return data.decode('utf-8') if EnvUtil.PY3 else data
  52. else:
  53. return None
  54. def put(self, proxy, num=1):
  55. """
  56. 将代理放入hash, 使用changeTable指定hash name
  57. :param proxy:
  58. :param num:
  59. :return:
  60. """
  61. data = self.__conn.hset(self.name, proxy, num)
  62. return data
  63. def delete(self, key):
  64. """
  65. Remove the ``key`` from hash ``name``
  66. :param key:
  67. :return:
  68. """
  69. self.__conn.hdel(self.name, key)
  70. def update(self, key, value):
  71. self.__conn.hincrby(self.name, key, value)
  72. def pop(self):
  73. """
  74. 弹出一个代理
  75. :return: dict {proxy: value}
  76. """
  77. proxies = self.__conn.hkeys(self.name)
  78. if proxies:
  79. proxy = random.choice(proxies)
  80. value = self.__conn.hget(self.name, proxy)
  81. self.delete(proxy)
  82. return {'proxy': proxy.decode('utf-8') if EnvUtil.PY3 else proxy,
  83. 'value': value.decode('utf-8') if EnvUtil.PY3 and value else value}
  84. return None
  85. def exists(self, key):
  86. return self.__conn.hexists(self.name, key)
  87. def getAll(self):
  88. item_dict = self.__conn.hgetall(self.name)
  89. if EnvUtil.PY3:
  90. return {key.decode('utf8'): value.decode('utf8') for key, value in item_dict.items()}
  91. else:
  92. return item_dict
  93. def getNumber(self):
  94. """
  95. Return the number of elements in hash ``name``
  96. :return:
  97. """
  98. return self.__conn.hlen(self.name)
  99. def changeTable(self, name):
  100. self.name = name
  101. if __name__ == '__main__':
  102. c = SsdbClient('useful_proxy', '118.24.52.95', 8899)
  103. print(c.getAll())