|
|
|
@ -11,70 +11,69 @@ class RedisClient(object):
|
|
|
|
|
:param port: 端口 |
|
|
|
|
:param password: 密码 |
|
|
|
|
""" |
|
|
|
|
self.db = redis.Redis(host=host, port=port, password=password, decode_responses=True) |
|
|
|
|
self.db = redis.StrictRedis(host=host, port=port, password=password, decode_responses=True) |
|
|
|
|
self.type = type |
|
|
|
|
self.website = website |
|
|
|
|
|
|
|
|
|
def key(self): |
|
|
|
|
def name(self): |
|
|
|
|
""" |
|
|
|
|
得到格式化的username |
|
|
|
|
:param username: 最后一个参数username |
|
|
|
|
:return: |
|
|
|
|
获取Hash的名称 |
|
|
|
|
:return: Hash名称 |
|
|
|
|
""" |
|
|
|
|
return "{type}:{website}".format(type=self.type, website=self.website) |
|
|
|
|
|
|
|
|
|
def set(self, username, value): |
|
|
|
|
""" |
|
|
|
|
设置键值对 |
|
|
|
|
:param username: |
|
|
|
|
:param value: |
|
|
|
|
:param username: 用户名 |
|
|
|
|
:param value: 密码或Cookies |
|
|
|
|
:return: |
|
|
|
|
""" |
|
|
|
|
return self.db.hset(self.key(), username, value) |
|
|
|
|
return self.db.hset(self.name(), username, value) |
|
|
|
|
|
|
|
|
|
def get(self, username): |
|
|
|
|
""" |
|
|
|
|
根据键名获取键值 |
|
|
|
|
:param username: |
|
|
|
|
:param username: 用户名 |
|
|
|
|
:return: |
|
|
|
|
""" |
|
|
|
|
return self.db.hget(self.key(), username) |
|
|
|
|
return self.db.hget(self.name(), username) |
|
|
|
|
|
|
|
|
|
def delete(self, username): |
|
|
|
|
""" |
|
|
|
|
根据键名删除键值对 |
|
|
|
|
:param username: |
|
|
|
|
:return: |
|
|
|
|
:param username: 用户名 |
|
|
|
|
:return: 删除结果 |
|
|
|
|
""" |
|
|
|
|
return self.db.hdel(self.key(), username) |
|
|
|
|
return self.db.hdel(self.name(), username) |
|
|
|
|
|
|
|
|
|
def count(self): |
|
|
|
|
""" |
|
|
|
|
获取数目 |
|
|
|
|
:return: 数目 |
|
|
|
|
""" |
|
|
|
|
return self.db.hlen(self.key()) |
|
|
|
|
return self.db.hlen(self.name()) |
|
|
|
|
|
|
|
|
|
def random(self): |
|
|
|
|
""" |
|
|
|
|
随机得到键值 |
|
|
|
|
:return: |
|
|
|
|
随机得到键值,用于随机Cookies获取 |
|
|
|
|
:return: 随机Cookies |
|
|
|
|
""" |
|
|
|
|
return random.choice(self.db.hvals(self.key())) |
|
|
|
|
return random.choice(self.db.hvals(self.name())) |
|
|
|
|
|
|
|
|
|
def usernames(self): |
|
|
|
|
""" |
|
|
|
|
获取所有账户信息 |
|
|
|
|
:return: |
|
|
|
|
:return: 所有用户名 |
|
|
|
|
""" |
|
|
|
|
return self.db.hkeys(self.key()) |
|
|
|
|
return self.db.hkeys(self.name()) |
|
|
|
|
|
|
|
|
|
def all(self): |
|
|
|
|
""" |
|
|
|
|
获取所有键值对 |
|
|
|
|
:return: |
|
|
|
|
:return: 用户名和密码或Cookies的映射表 |
|
|
|
|
""" |
|
|
|
|
return self.db.hgetall(self.key()) |
|
|
|
|
return self.db.hgetall(self.name()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|