SsdbClient.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. def __init__(self, name, **kwargs):
  29. """
  30. init
  31. :param name: hash name
  32. :param host: host
  33. :param port: port
  34. :param password: password
  35. :return:
  36. """
  37. self.name = name
  38. self.__conn = Redis(connection_pool=BlockingConnectionPool(**kwargs))
  39. def get(self, proxy):
  40. """
  41. get an item
  42. 从hash中获取对应的proxy, 使用前需要调用changeTable()
  43. :param proxy:
  44. :return:
  45. """
  46. data = self.__conn.hget(name=self.name, key=proxy)
  47. if data:
  48. return data.decode('utf-8') if EnvUtil.PY3 else data
  49. else:
  50. return None
  51. def put(self, proxy, num=1):
  52. """
  53. 将代理放入hash, 使用changeTable指定hash name
  54. :param proxy:
  55. :param num:
  56. :return:
  57. """
  58. data = self.__conn.hset(self.name, proxy, num)
  59. return data
  60. def delete(self, key):
  61. """
  62. Remove the ``key`` from hash ``name``
  63. :param key:
  64. :return:
  65. """
  66. self.__conn.hdel(self.name, key)
  67. def update(self, key, value):
  68. self.__conn.hincrby(self.name, key, value)
  69. def pop(self):
  70. """
  71. 弹出一个代理
  72. :return: dict {proxy: value}
  73. """
  74. proxies = self.__conn.hkeys(self.name)
  75. if proxies:
  76. proxy = random.choice(proxies)
  77. value = self.__conn.hget(self.name, proxy)
  78. self.delete(proxy)
  79. return {'proxy': proxy.decode('utf-8') if EnvUtil.PY3 else proxy,
  80. 'value': value.decode('utf-8') if EnvUtil.PY3 and value else value}
  81. return None
  82. def exists(self, key):
  83. return self.__conn.hexists(self.name, key)
  84. def getAll(self):
  85. item_dict = self.__conn.hgetall(self.name)
  86. if EnvUtil.PY3:
  87. return {key.decode('utf8'): value.decode('utf8') for key, value in item_dict.items()}
  88. else:
  89. return item_dict
  90. def getNumber(self):
  91. """
  92. Return the number of elements in hash ``name``
  93. :return:
  94. """
  95. return self.__conn.hlen(self.name)
  96. def changeTable(self, name):
  97. self.name = name
  98. if __name__ == '__main__':
  99. c = SsdbClient(name='useful_proxy', host='127.0.0.1', port=8899, password=None)
  100. print(c.getAll())