|
|
@@ -15,20 +15,19 @@
|
|
|
"""
|
|
|
__author__ = 'JHao'
|
|
|
|
|
|
-from Util import EnvUtil
|
|
|
+from Config.setting import PY3
|
|
|
|
|
|
from redis.connection import BlockingConnectionPool
|
|
|
from redis import Redis
|
|
|
-import random
|
|
|
|
|
|
|
|
|
class SsdbClient(object):
|
|
|
"""
|
|
|
SSDB client
|
|
|
|
|
|
- SSDB中代理存放的容器为hash:
|
|
|
- 原始代理存放在name为raw_proxy的hash中,key为代理的ip:port,value为None,以后扩展可能会加入代理属性;
|
|
|
- 验证后的代理存放在name为useful_proxy的hash中,key为代理的ip:port,value为一个计数,初始为1,每校验失败一次减1;
|
|
|
+ SSDB中代理存放的结构为hash:
|
|
|
+ 原始代理存放在name为raw_proxy的hash中, key为代理的ip:por, value为代理属性的字典;
|
|
|
+ 验证后的代理存放在name为useful_proxy的hash中, key为代理的ip:port, value为代理属性的字典;
|
|
|
|
|
|
"""
|
|
|
def __init__(self, name, **kwargs):
|
|
|
@@ -43,75 +42,100 @@ class SsdbClient(object):
|
|
|
self.name = name
|
|
|
self.__conn = Redis(connection_pool=BlockingConnectionPool(**kwargs))
|
|
|
|
|
|
- def get(self, proxy):
|
|
|
+ def get(self, proxy_str):
|
|
|
"""
|
|
|
- get an item
|
|
|
从hash中获取对应的proxy, 使用前需要调用changeTable()
|
|
|
- :param proxy:
|
|
|
+ :param proxy_str: proxy str
|
|
|
:return:
|
|
|
"""
|
|
|
- data = self.__conn.hget(name=self.name, key=proxy)
|
|
|
+ data = self.__conn.hget(name=self.name, key=proxy_str)
|
|
|
if data:
|
|
|
- return data.decode('utf-8') if EnvUtil.PY3 else data
|
|
|
+ return data.decode('utf-8') if PY3 else data
|
|
|
else:
|
|
|
return None
|
|
|
|
|
|
- def put(self, proxy, num=1):
|
|
|
+ def put(self, proxy_obj):
|
|
|
"""
|
|
|
将代理放入hash, 使用changeTable指定hash name
|
|
|
- :param proxy:
|
|
|
- :param num:
|
|
|
+ :param proxy_obj: Proxy obj
|
|
|
:return:
|
|
|
"""
|
|
|
- data = self.__conn.hset(self.name, proxy, num)
|
|
|
+ data = self.__conn.hset(self.name, proxy_obj.proxy, proxy_obj.info_json)
|
|
|
return data
|
|
|
|
|
|
- def delete(self, key):
|
|
|
+ def delete(self, proxy_str):
|
|
|
"""
|
|
|
- Remove the ``key`` from hash ``name``
|
|
|
- :param key:
|
|
|
+ 移除指定代理, 使用changeTable指定hash name
|
|
|
+ :param proxy_str: proxy str
|
|
|
:return:
|
|
|
"""
|
|
|
- self.__conn.hdel(self.name, key)
|
|
|
+ self.__conn.hdel(self.name, proxy_str)
|
|
|
|
|
|
- def update(self, key, value):
|
|
|
- self.__conn.hincrby(self.name, key, value)
|
|
|
+ def exists(self, proxy_str):
|
|
|
+ """
|
|
|
+ 判断指定代理是否存在, 使用changeTable指定hash name
|
|
|
+ :param proxy_str: proxy str
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ return self.__conn.hexists(self.name, proxy_str)
|
|
|
+
|
|
|
+ def update(self, proxy_obj):
|
|
|
+ """
|
|
|
+ 更新 proxy 属性
|
|
|
+ :param proxy_obj:
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ self.__conn.hset(self.name, proxy_obj.proxy, proxy_obj.info_json)
|
|
|
|
|
|
def pop(self):
|
|
|
"""
|
|
|
弹出一个代理
|
|
|
:return: dict {proxy: value}
|
|
|
"""
|
|
|
- proxies = self.__conn.hkeys(self.name)
|
|
|
- if proxies:
|
|
|
- proxy = random.choice(proxies)
|
|
|
- value = self.__conn.hget(self.name, proxy)
|
|
|
- self.delete(proxy)
|
|
|
- return {'proxy': proxy.decode('utf-8') if EnvUtil.PY3 else proxy,
|
|
|
- 'value': value.decode('utf-8') if EnvUtil.PY3 and value else value}
|
|
|
+ # proxies = self.__conn.hkeys(self.name)
|
|
|
+ # if proxies:
|
|
|
+ # proxy = random.choice(proxies)
|
|
|
+ # value = self.__conn.hget(self.name, proxy)
|
|
|
+ # self.delete(proxy)
|
|
|
+ # return {'proxy': proxy.decode('utf-8') if PY3 else proxy,
|
|
|
+ # 'value': value.decode('utf-8') if PY3 and value else value}
|
|
|
return None
|
|
|
|
|
|
- def exists(self, key):
|
|
|
- return self.__conn.hexists(self.name, key)
|
|
|
-
|
|
|
def getAll(self):
|
|
|
+ """
|
|
|
+ 列表形式返回所有代理, 使用changeTable指定hash name
|
|
|
+ :return:
|
|
|
+ """
|
|
|
item_dict = self.__conn.hgetall(self.name)
|
|
|
- if EnvUtil.PY3:
|
|
|
- return {key.decode('utf8'): value.decode('utf8') for key, value in item_dict.items()}
|
|
|
+ if PY3:
|
|
|
+ return [value.decode('utf8') for key, value in item_dict.items()]
|
|
|
else:
|
|
|
- return item_dict
|
|
|
+ return item_dict.values()
|
|
|
+
|
|
|
+ def clear(self):
|
|
|
+ """
|
|
|
+ 清空所有代理, 使用changeTable指定hash name
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ return self.__conn.execute_command("hclear", self.name)
|
|
|
|
|
|
def getNumber(self):
|
|
|
"""
|
|
|
- Return the number of elements in hash ``name``
|
|
|
+ 返回代理数量
|
|
|
:return:
|
|
|
"""
|
|
|
return self.__conn.hlen(self.name)
|
|
|
|
|
|
def changeTable(self, name):
|
|
|
+ """
|
|
|
+ 切换操作对象
|
|
|
+ :param name: raw_proxy/useful_proxy
|
|
|
+ :return:
|
|
|
+ """
|
|
|
self.name = name
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- c = SsdbClient(name='useful_proxy', host='127.0.0.1', port=8899, password=None)
|
|
|
- print(c.getAll())
|
|
|
+ c = SsdbClient(name='raw_proxy', host='120.79.78.193', port=8888, password=None)
|
|
|
+ c.get("103.194.233.188:80810")
|
|
|
+ print(c.clear())
|