class SSDBKV(object): def __init__(self, host="127.0.0.1", port=8888, max_connections=10, timeout=60): self.host = host self.port = port self.max_connections = max_connections self.timeout = timeout pool = BlockingConnectionPool(connection_class=Connection, max_connections=max_connections, timeout=timeout, host=host, port=port) self.ssdb = SSDB(connection_pool=pool) def set(self, key, value): return self.ssdb.set(key, value) def get(self, key): return self.ssdb.get(key) def delete(self, key): return self.ssdb.delete(key) def keys(self, name_start=0, name_end=0xFFFFFFFF, limit=10): return self.ssdb.keys(name_start, name_end, limit) def exists(self, key): return self.ssdb.exists(key)
def quary(): keyword = "weibo_拾笔不良_2017-05-08" try: client = SSDB(host='', port=8884) #连接到服务器的SSDB数据库 res = client.get(keyword) except Exception as e: print e.message results = [] else: results = json.loads(res) finally: print len(results) for block in results: print "++++++++++++++" print "name:", block['name'] " *** 重要 *** " print "content:", urllib.unquote(str( block['content'])) #首先将结果转换为字符串,然后对字符串进行url解码 print "point:", block['point'] comment_list = block['comment_list'] for comment in comment_list: print "comment:", comment
def getIP(): client = SSDB(host='10.141.5.89', port=8884) keyword = "proxy" res = client.get(keyword) addr_list = list() ip_port_list = res.split(",")[0:-2] """拆分出IP和Port""" for ip_port in ip_port_list: ip, port = re.split("\s+", ip_port) addr_list.append(ip + ":" + port) print "addr parse successful" return addr_list
r = SSDB(host='10.68.120.175', port=7777, password='******') # r = SSDB.from_url(url='ssdb://:[email protected]:7777') # **Server** # b = r.auth('11111111111111111111111111111111') # b=r.info('cmd') # b= r.dbsize() # b = r.flushdb() # **Key Value** # b=r.set('aa',11) # 1 # b=r.setx('aa',11,1000) # 1 # b = r.setnx('bds', 11) # 1/0 # b = r.expire('aaqe',1000) # 1/0 # b = r.ttl('bdasd') # int value b = r.get('bdas') # value/none # b = r.getset('bdadssf','22') # value/none # b = r.delete('aa') # 1 # b = r.incr('aaaasdfsd',2) # int value # b = r.exists('aaa') # true/false # b = r.getbit('14aaasdf',11) # 1/0 # b = r.setbit('asfga',11112341, 1) # 1/0 # b = r.bitcount('aavba', 0,1) # 1/0 # b = r.countbit('aa',0,-1) # 1/0 # b = r.substr('bdadssf',0,-1) # value # b = r.strlen('asfga') # int value # b = r.keys('','aavba',10) # list # b = r.rkeys('','',10) # list # b = r.scan('','',-1) # dict # b = r.rscan('', '', -1) # dict # kvs = {
global ssdb try: ssdb.set(contestID, rank_list_proto_str) except: pass def InsertUserProblemStatus(userID, problemID, status): #AC == 1 No_pass = 2 other = 0 global ssdb value = 2 if status == "Accepted": value = 1 st = str(ssdb.get(userID + '\t' + str(problemID))) if st == "1": return; else: ssdb.set(userID + '\t' + str(problemID), value) def GetUserProblem(userID, problemID): global ssdb st = ssdb.get(userID + '\t' + str(problemID)) if str(st) != "1" and str(st) != "2": return "0" else: return str(st) if __name__ == '__main__': ssdb = SSDB(host='127.0.0.1', port=6666) print ssdb.get("QQ 1015")
ssdb.set(contestID, rank_list_proto_str) except: pass def InsertUserProblemStatus(userID, problemID, status): #AC == 1 No_pass = 2 other = 0 global ssdb value = 2 if status == "Accepted": value = 1 st = str(ssdb.get(userID + '\t' + str(problemID))) if st == "1": return else: ssdb.set(userID + '\t' + str(problemID), value) def GetUserProblem(userID, problemID): global ssdb st = ssdb.get(userID + '\t' + str(problemID)) if str(st) != "1" and str(st) != "2": return "0" else: return str(st) if __name__ == '__main__': ssdb = SSDB(host='127.0.0.1', port=6666) print ssdb.get("QQ 1015")
class QueueSSDB(QueueBase.QueueBase): """ base class , only provide interface for sub class to implement """ def __init__(self, name, host='localhost', port=8888, **kwargs): QueueBase.QueueBase.__init__(self, name, host, port) self.__conn = SSDB(connection_pool=BlockingConnectionPool(host=self.host, port=self.port)) #queue @QueueBase.catch def put(self, value, *args, **kwargs): """ put an item in the back of a queue :param value: :param args: :param kwargs: :return: """ return self.__conn.qpush_back(self.name, json.dumps(value, ensure_ascii=False).encode('utf-8') if isinstance(value, dict) or isinstance(value, list) else value) def save(self, value, *args, **kwargs): """ put an item in the back of a queue :param value: :param args: :param kwargs: :return: """ return self.__conn.qpush_back(self.name, json.dumps(value, ensure_ascii = False).encode('utf-8') if isinstance(value, dict) or isinstance( value, list) else value) @QueueBase.catch def get(self, *args, **kwargs): """ get element from the from of queue :param args: :param kwargs: :return: """ value = self.__conn.qpop_front(self.name) return value[0] if value else value @QueueBase.catch def getMore(self, *args, **kwargs): """ get element from the from of queue :param args: :param kwargs: :return: """ value = self.__conn.qpop_front(self.name, **kwargs) return value @QueueBase.catch def size(self, *args, **kwargs): return self.__conn.qsize(self.name) @QueueBase.catch def changeTable(self, name): """ change the queue name to operate :param name: :return: """ self.name = name @QueueBase.catch def select_queue(self, name): """ change the queue name to operate :param name: :return: """ self.name = name @QueueBase.catch def qclaerQueue(self): return self.__conn.qclear(self.name) #KV @QueueBase.catch def keySet(self,key,value): """ Set the value at key ``name`` to ``value`` . :param key: :param value: :return: """ value = json.dumps(value, ensure_ascii = False).encode('utf-8') if isinstance(value,dict) or isinstance(value, list) else value return self.__conn.set(key,value) @QueueBase.catch def keySetx(self,name, value, ttl=-1): """ Set the value of key ``name`` to ``value`` that expires in ``ttl`` seconds. ``ttl`` can be represented by an integer or a Python timedelta object. :param name: :param value: :param ttl: :return: """ return self.__conn.setx(name,value,ttl=ttl) @QueueBase.catch def keyTtl(self,key): """ Returns the number of seconds until the key ``name`` will expire. :return: """ self.__conn.ttl(key) @QueueBase.catch def keyGet(self,key): """ Return the value at key ``name``, or ``None`` if the key doesn't exist :param key: :return: """ return self.__conn.get(key) @QueueBase.catch def keyDel(self,key): """ Delete the key specified by ``name`` . :param key: :return: """ return self.__conn.delete(key) @QueueBase.catch def keyKeys(self,key_start='',key_end=''): """ Return a list of the top ``limit`` keys between ``name_start`` and ``name_end`` :param key_start: :param key_end: :return: """ return self.__conn.keys(name_start=key_start,name_end=key_end,limit=100000) @QueueBase.catch def keyexists(self,key): """ :param key: :return: """ return self.__conn.exists(key) #SET @QueueBase.catch def zsetSet(self,field,score = 1): if field: if isinstance(field, dict) or isinstance(field, list): field = json.dumps(field) field = field if len(field) < 100 else field[:100] return self.__conn.zset(self.name, field, score) @QueueBase.catch def zgetSet(self,key): return self.__conn.zget(self.name,key) @QueueBase.catch def zexistsSet(self,name,field): return self.__conn.zexists(name,field) @QueueBase.catch def zkeysSet(self): return self.__conn.zkeys(self.name,'','','',limit=100000000) @QueueBase.catch def zdelSet(self,key): return self.__conn.zdel(self.name,key) @QueueBase.catch def multi_zgetSet(self,*keys): return self.__conn.multi_zget(self.name,*keys) #Hash @QueueBase.catch def hgetallHash(self,key): return self.__conn.hgetall(key) @QueueBase.catch def hincrHash(self,name,key): return self.__conn.hincr(name,key,amount=1) @QueueBase.catch def multi_hsetHash(self,name,**mapping): return self.__conn.multi_hset(name, **mapping) @QueueBase.catch def hlistHash(self,start,end): return self.__conn.hlist(start, end, limit = 10000000) @QueueBase.catch def hclearHash(self,key): return self.__conn.hclear(key) @QueueBase.catch def hset(self, key,value): return self.__conn.hset(self.name, key,json.dumps(value, ensure_ascii = False).encode('utf-8') if isinstance(value,dict) or isinstance(value, list) else value) @QueueBase.catch def hsize(self): return self.__conn.hsize(self.name) @QueueBase.catch def hget(self, key = None): if key: return self.__conn.hget(self.name, key) else: if self.__conn.hsize(self.name) > 0: keys=self.__conn.hkeys(self.name,"", "",limit=1) if keys: key=keys[0] v=self.__conn.hget(self.name, key) self.__conn.hdel(self.name, key) return v