예제 #1
0
def get_session_data(session_key):
    client = get_redis_client()
    
    if not session_key:
        return {}
    
    data = client.get('session:%s' % session_key)
    if not data:
        return {}

    return pickle.loads(data)
예제 #2
0
파일: gtalk.py 프로젝트: blueicesir/utils
    def message(self,stanza):
        subject=stanza.get_subject()
        body=stanza.get_body()
        t=stanza.get_type()
        print u'收到[%s]的信息.' % (unicode(stanza.get_from(),)),
        if subject:
            print u'主题:"%s"' % (subject,),
        if body:
            print u'正文:"%s"' % (body,),
        if t:
            print u'类型:"%s"' % (t,)
        else:
            print u'类型:"正常"'


        if t=="chat" and body==None:
            return True


        if stanza.get_type()=='headline':
            return True

        if subject:
            subject=u'Re: '+subject


        if self.ini:
            db_host=self.ini['redis_host']
            db_port=self.ini['redis_port']
            db_num=self.ini['redis_dbid']
            db_auth=self.ini['redis_auth']
#            print "Redis DB %s:%s database index %s auth %s" %(db_host,db_port,db_num,db_auth)
        else:
            print "Redis DB not configuration"
            sys.exit(0)


        try:
            cmd=body.lower()    # 转换为小写
            regexp=re.compile('(get|keys)\s(.*)',re.I)  # 正则匹配不区分大小写
            key=regexp.match(body)  # 由于直接访问group(2),如果成员不存在会抛出异常
            if key:
                key=key.group(2)
                db_num=6    # 设置数据库

            if cmd.startswith('get'):
                client=redis.Redis(host=db_host,port=db_port,db=db_num,password=db_auth)
                if key:
                    if client.exists(key):
                        body=client.get(key)
                    else:
                        body="get key not exists."
                else:
                    body="get execute fail"
            elif cmd.startswith('keys'):
                client=redis.Redis(host=db_host,port=db_port,db=db_num,password=db_auth)
                if key:
                    body=client.keys(key)
                    if len(body)!=0:
                        ret="\r".join(body)
                        body=ret
                    else:
                        body="key is not exists."
                else:
                    body="keys execute fail"
            else:
                print 'search chinese name'
                db_num=4
                client=redis.Redis(host=db_host,port=db_port,db=db_num,password=db_auth)
                body+=(":"+client.get(body).decode('utf-8'))
        except Exception,e: # 这种写法是不安全的,这样会淹没所有try到except中的异常,而且看不到那里出现的错误。
            body=u'您查询的"%s"无记录。' % (body,)