SsdbClient.py 3.2 KB

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