|
@@ -9,7 +9,7 @@
|
|
|
-------------------------------------------------
|
|
-------------------------------------------------
|
|
|
Change Activity:
|
|
Change Activity:
|
|
|
2016/12/2:
|
|
2016/12/2:
|
|
|
- 2017/04/26: 添加get_status方法获取hash长度
|
|
|
|
|
|
|
+ 2017/09/22: PY3中 redis-py返回的数据是bytes型
|
|
|
-------------------------------------------------
|
|
-------------------------------------------------
|
|
|
"""
|
|
"""
|
|
|
__author__ = 'JHao'
|
|
__author__ = 'JHao'
|
|
@@ -19,7 +19,6 @@ from Util import EnvUtil
|
|
|
from redis.connection import BlockingConnectionPool
|
|
from redis.connection import BlockingConnectionPool
|
|
|
from redis import Redis
|
|
from redis import Redis
|
|
|
import random
|
|
import random
|
|
|
-import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SsdbClient(object):
|
|
class SsdbClient(object):
|
|
@@ -27,8 +26,8 @@ class SsdbClient(object):
|
|
|
SSDB client
|
|
SSDB client
|
|
|
|
|
|
|
|
SSDB中代理存放的容器为hash:
|
|
SSDB中代理存放的容器为hash:
|
|
|
- 原始代理存放在name为raw_proxy的hash中,key为代理的ip:port,value为None,以后扩展可能会加入代理属性;
|
|
|
|
|
- 验证后供flask使用的代理存放在name为useful_proxy的hash中,key为代理的ip:port,value为None,以后扩展可能会加入代理属性;
|
|
|
|
|
|
|
+ 原始代理存放在name为raw_proxy的hash中,key为代理的ip:port,value为为None,以后扩展可能会加入代理属性;
|
|
|
|
|
+ 验证后的代理存放在name为useful_proxy的hash中,key为代理的ip:port,value为一个计数,初始为1,每校验失败一次减1;
|
|
|
|
|
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
@@ -43,43 +42,28 @@ class SsdbClient(object):
|
|
|
self.name = name
|
|
self.name = name
|
|
|
self.__conn = Redis(connection_pool=BlockingConnectionPool(host=host, port=port))
|
|
self.__conn = Redis(connection_pool=BlockingConnectionPool(host=host, port=port))
|
|
|
|
|
|
|
|
- def get(self):
|
|
|
|
|
|
|
+ def get(self, proxy):
|
|
|
"""
|
|
"""
|
|
|
get an item
|
|
get an item
|
|
|
-
|
|
|
|
|
- 从useful_proxy_queue随机获取一个可用代理, 使用前需要调用changeTable("useful_proxy_queue")
|
|
|
|
|
|
|
+ 从hash中获取对应的proxy, 使用前需要调用changeTable()
|
|
|
|
|
+ :param proxy:
|
|
|
:return:
|
|
:return:
|
|
|
"""
|
|
"""
|
|
|
- values = self.__conn.hkeys(name=self.name)
|
|
|
|
|
- keys = list(values) if EnvUtil.PY3 else values
|
|
|
|
|
- return random.choice(keys) if values else None
|
|
|
|
|
|
|
+ data = self.__conn.hget(name=self.name, key=proxy)
|
|
|
|
|
+ if data:
|
|
|
|
|
+ return data.decode('utf-8') if EnvUtil.PY3 else data
|
|
|
|
|
+ else:
|
|
|
|
|
+ return None
|
|
|
|
|
|
|
|
- def put(self, key):
|
|
|
|
|
|
|
+ def put(self, proxy, num=1):
|
|
|
"""
|
|
"""
|
|
|
- put an item
|
|
|
|
|
-
|
|
|
|
|
将代理放入hash, 使用changeTable指定hash name
|
|
将代理放入hash, 使用changeTable指定hash name
|
|
|
- :param key:
|
|
|
|
|
|
|
+ :param proxy:
|
|
|
|
|
+ :param num:
|
|
|
:return:
|
|
:return:
|
|
|
"""
|
|
"""
|
|
|
- key = json.dump(key, ensure_ascii=False) if isinstance(key, (dict, list)) else key
|
|
|
|
|
- return self.__conn.hincrby(self.name, key, 1)
|
|
|
|
|
-
|
|
|
|
|
- def getvalue(self, key):
|
|
|
|
|
- value = self.__conn.hget(self.name, key)
|
|
|
|
|
- return value if value else None
|
|
|
|
|
-
|
|
|
|
|
- def pop(self):
|
|
|
|
|
- """
|
|
|
|
|
- pop an item
|
|
|
|
|
-
|
|
|
|
|
- 弹出一个代理, 使用changeTable指定hash name
|
|
|
|
|
- :return:
|
|
|
|
|
- """
|
|
|
|
|
- key = self.get()
|
|
|
|
|
- if key:
|
|
|
|
|
- self.__conn.hdel(self.name, key)
|
|
|
|
|
- return key
|
|
|
|
|
|
|
+ data = self.__conn.hincrby(self.name, proxy, num)
|
|
|
|
|
+ return data
|
|
|
|
|
|
|
|
def delete(self, key):
|
|
def delete(self, key):
|
|
|
"""
|
|
"""
|
|
@@ -89,14 +73,28 @@ class SsdbClient(object):
|
|
|
"""
|
|
"""
|
|
|
self.__conn.hdel(self.name, key)
|
|
self.__conn.hdel(self.name, key)
|
|
|
|
|
|
|
|
- def inckey(self, key, value):
|
|
|
|
|
|
|
+ def update(self, key, value):
|
|
|
self.__conn.hincrby(self.name, key, value)
|
|
self.__conn.hincrby(self.name, key, value)
|
|
|
|
|
|
|
|
|
|
+ def pop(self):
|
|
|
|
|
+ proxies = self.__conn.hkeys(self.name)
|
|
|
|
|
+ if proxies:
|
|
|
|
|
+ proxy = random.choice(proxies)
|
|
|
|
|
+ self.delete(proxy)
|
|
|
|
|
+ return proxy
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+ def exists(self, key):
|
|
|
|
|
+ return self.__conn.hexists(self.name, key)
|
|
|
|
|
+
|
|
|
def getAll(self):
|
|
def getAll(self):
|
|
|
- keys = self.__conn.hkeys(self.name)
|
|
|
|
|
- return list(keys) if EnvUtil.PY3 else keys
|
|
|
|
|
|
|
+ item_dict = self.__conn.hgetall(self.name)
|
|
|
|
|
+ if EnvUtil.PY3:
|
|
|
|
|
+ return {key.decode('utf8'): value.decode('utf8') for key, value in item_dict.items()}
|
|
|
|
|
+ else:
|
|
|
|
|
+ return item_dict
|
|
|
|
|
|
|
|
- def get_status(self):
|
|
|
|
|
|
|
+ def getNumber(self):
|
|
|
"""
|
|
"""
|
|
|
Return the number of elements in hash ``name``
|
|
Return the number of elements in hash ``name``
|
|
|
:return:
|
|
:return:
|