Example #1
0
    def image_search(self, request, pk=None):
        redis_connection = Redis()
        course = self.get_object()

        search_result = redis_connection.get(hash(course))
        if not search_result:
            search_string = str(course)
            search_token = re.findall(
                r'vqd=\'(.*)\';',
                requests.post("https://duckduckgo.com/",
                              data={
                                  'q': search_string
                              }).text)[0]
            search_result = requests.get(
                "https://duckduckgo.com/i.js?l=it-it&o=json&vqd=%s&q=%s&f=,,,&p=2"
                % (search_token, search_string),
                headers={
                    'Dnt': '1',
                    'X-Requested-With': 'XMLHttpRequest',
                    'User-Agent':
                    'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Referer': 'https://duckduckgo.com/',
                    'Authority': 'duckduckgo.com',
                    'Host': 'duckduckgo.com',
                    'Connection': 'keep-alive',
                    'Cache-Control': 'no-cache',
                }).text
            redis_connection.append(hash(course), search_result)
        redis_connection.close()
        return Response(json.loads(search_result)['results'][0])
Example #2
0
def do_register():
    username = request.form.get("username")
    password = request.form.get("password")
    if username and password:
        r = Redis()
        flag = r.get(username)
        if flag:
            flash("该用户名已存在", "register_err")
            return redirect(url_for("register"))
        else:
            r.append(username, password)
            message = "恭喜你,注册成功"
            flash(message)
            return render_template("success.html", url=url_for("login"))
    else:
        flash("账号或密码不能为空", "register_err")
        return redirect(url_for("register"))
Example #3
0
def copy_logs_to_redis(conn: Redis,
                       path,
                       channel,
                       count=10,
                       limit=2**30,
                       quit_when_done=True):
    bytes_in_redis = 0
    waiting = deque()
    # 创建用于向客户端发送消息的群组
    create_chat(conn, "source", map(str, range(count)), "", channel)
    count = str(count)
    # 遍历所有日志文件
    for logfile in sorted(os.listdir(path)):
        full_path = os.path.join(path, logfile)
        file_size = os.stat(full_path).st_size
        # 如果程序需要更多的空间,那么清除已处理完毕的文件
        while bytes_in_redis + file_size > limit:
            cleaned = _clean(conn, channel, waiting, count)
            if cleaned:
                bytes_in_redis -= cleaned
            else:
                time.sleep(0.25)
        # 将文件上传到Redis
        with open(full_path, "rb") as inp:
            block = " "
            while block:
                block = inp.read(2**17)
                conn.append(channel + logfile, block)
        # 提醒监听者,文件已经准备就绪
        send_message(conn, channel, "source", logfile)

        # 对本地记录的Redis内存占用量相关信息进行更新
        bytes_in_redis += file_size
        waiting.append((logfile, file_size))

    # 所有日志文件已经处理完毕,向监听者报告此事
    if quit_when_done:
        send_message(conn, channel, "source", ":done")

    # 工作完成,清理无用日志
    while waiting:
        cleaned = _clean(conn, channel, waiting, count)
        if cleaned:
            bytes_in_redis -= cleaned
        else:
            time.sleep(0.25)
Example #4
0
def test_work_in_parallel_to_redis():
    "HTTPretty should passthrough redis connections"

    redis = Redis()

    keys = redis.keys('*')
    for key in keys:
        redis.delete(key)

    redis.append('item1', 'value1')
    redis.append('item2', 'value2')

    sorted(redis.keys('*')).should.equal([b'item1', b'item2'])

    httpretty.register_uri(httpretty.GET, "http://redis.io", body="salvatore")

    response = requests.get('http://redis.io')
    response.text.should.equal('salvatore')
Example #5
0
from redis import Redis  #importowanie biblioteki z redisa

redis_connection = Redis(
    decode_responses=True)  #zmienna sprawdzająca czy jest połączenie
print(redis_connection.ping(),
      "- Połączono")  #wypisanie w konsoli, czy połaczono z redisem

##STRING##

#Dekleracja zmiennych
key = "some-key"
value = "some-val"
value1 = 55
#Przypisanie oraz wypisanie wartości key
redis_connection.set(key, value)
print("Wartość key to:", redis_connection.get(key))
#Do wartości key dodanie dowolnej frazy
redis_connection.append(key, "-dodana fraza")
print("Wartość key z dodaną frazą to:", redis_connection.get(key))
#Usuwanie wartości key
redis_connection.delete(key)
print("Wartość key po usunięciu to:", redis_connection.get(key))

#Przypisanie zmiennej key innej wartości(następuje nadpisanie)liczbowej
redis_connection.set(key, value1)
#Wypisanie liczbowej wartości key
print("Wartość key to:", redis_connection.get(key))
#Zwiększenie wartości key o 5
print("Wartość key + 5 to:", redis_connection.incr(key, 5))
#Zmniejszenie wartości key o 20
print("Wartość key -20 to:", redis_connection.decr(key, 20))
 def _publish(self, content):
     r = Redis()
     id_ = self._generate_id(8)
     r.append(City.KEY, "\n" + json.dumps(content))
def set_up_redis(query):
    redis_client = Redis(host=settings.RQ_QUEUES["default"]["HOST"],
                         port=settings.RQ_QUEUES["default"]["PORT"])
    redis_client.append(query.api_session_id(), 'start')
    redis_client.expire(query.api_session_id(), 10)
Example #8
0
class GoogleAnalyticsManager:

    @classmethod
    def using_settings(cls):
        ga_id = ''
        ga_settings = GoogleAnalyticsSettings.objects.first()
        if ga_settings is not None:
            ga_id = ga_settings.ga_id

        return cls(tracking_id=ga_id)

    def __init__(self, tracking_id):
        self.session = requests.session()
        self.tracking_id = tracking_id
        self.redis_client = Redis(host=settings.REDIS_HOST,
                                  port=settings.REDIS_PORT)

    @staticmethod
    def prepare_send_analytics(created, instance, **_):
        if (not is_options_request(instance)) and created:
            query = instance

            GoogleAnalyticsManager.using_settings().manage_query(query)

    def manage_query(self, query):
        regex = query.api_data.kongapipluginhttplog.exclude_regex
        if not regex \
                or re.search(regex, query.uri) is None:
            self.send_analytics(query)

    def generate_payload(self, cid, query):
        data = {'v': 1,  # Protocol Version
                'cid': cid,  # Client ID
                'tid': self.tracking_id,  # Tracking ID
                'uip': query.ip_address,  # User IP override
                't': 'pageview',  # Hit Type
                'dh': query.host,  # Document HostName
                'dp': query.uri,  # Document Path
                'cd1': query.querystring,  # Custom Dimention
                'cm1': query.start_time,  # Custom Metric
                'srt': query.request_time,  # Server Response Time
                'cm2': query.status_code,  # Custom Metric
                'cd3': query.api_data.name,
                'cm3': query.api_data.pk,
                'ua': query.user_agent}

        if not self.redis_client.exists(query.api_session_id()):
            data['sc'] = 'start'  # this request starts a new session

        min_to_timeout = GoogleAnalyticsSettings.get_solo().max_timeout
        self.redis_client.append(query.api_session_id(), 'start')
        self.redis_client.expire(query.api_session_id(), min_to_timeout*60)

        return data

    def send_analytics(self, query):
        if query.token is None:
            cid = query.ip_address + query.user_agent
        else:
            cid = query.token

        cid = hashlib.sha1(cid.encode()).digest()
        cid = str(uuid.UUID(bytes=cid[:16]))
        data = self.generate_payload(cid, query)

        response = self.session.post('http://www.google-analytics.com/collect', data=data)
        if not response.ok:
            raise ConnectionError(response.content)
Example #9
0
from redis import Redis

redis_connection = Redis(decode_responses=True) 

key ="x"
value ="test"

redis_connection.set(key, value)
print(redis_connection.get(key))

redis_connection.append(key, " redisa")
print(redis_connection.get(key))

redis_connection.delete(key)
print(redis_connection.get(key))
from redis import Redis

redis_connection = Redis(decode_responses=True)

key = "kluczv3"
value = "wartoscv3"

redis_connection.set(key, value)
print(redis_connection.get(key))

redis_connection.append(key, "   append")
print(redis_connection.get(key))

#redis_connection.delete(key)
#print(redis_connection.get(key))
Example #11
0
from redis import Redis

redis_connection = Redis(decode_responses=True)

# przypisanie własnych wartości
klucz = "jagoda"
wartosc = "styczen"

# odczytanie wartości
redis_connection.set(klucz, wartosc)
print(redis_connection.get(klucz))

# dodanie wartości poprzez append
redis_connection.append(klucz, "-teraz")
print(redis_connection.get(klucz))

# usunięcie klucza poprzez delete
redis_connection.delete(klucz)
print(redis_connection.get(klucz))
Example #12
0
conn.scard('key')
conn.smembers('key')
conn.smove('key1', 'key2', 'item')
conn.sdiff('key1', 'key2', 'key3') # 返回存在第一个集合,不在其他集合的元素
conn.sinter('key1', 'key2')
conn.sunion('key1', 'key2',)

# string
conn.set('key', '15')
conn.get('key')
conn.incr('key') # conn.incr('key', 1)
conn.incr('key', 5)
conn.decr('key', 5)
conn.incrbyfloat('key')
conn.incrbyfloat('key', -4.5)
conn.append('key', ' world')
conn.substr('key', 0, -2)
conn.setrange('key', 11, ' world!')

# zset
conn.zadd('zkey', 'member', 10, 'member1', 20)
conn.zrem('zkey', 'member1')
conn.zcard('zkey')
conn.zincrby('zkey', 'member', 10)
conn.zcount('zkey', 10, 20)
conn.zrank('zkey', 'member') # 分值从小到大排列
conn.zscore('zkey', 'member')
conn.zrange('zkey', 0, 9, withscores=True) # 返回前10名的成员和分数
conn.zinterstore('zset-u', ['zset1', 'zset2'], aggregate='sum') # 成员的交集,聚合函数默认为sum,成员排名相加后为新的排名
conn.zunionstore('zset-u', ['zset1', 'zset2'], aggregate='min') # 成员的并集,聚合函数为min取最小值为新的排名
Example #13
0
class ZWRedis():
    """Class defining a Redis driver"""
    def __init__(self, db_url, **kwargs):
        self.db_url = db_url or os.environ.get('DATABASE_URL')
        if not self.db_url:
            raise ValueError('You must provide a db_url.')

        o = utils.db_url_parser(db_url)
        self.dbcfg = {
            'host'      : o['host'],
            'port'      : o['port'] or 6379,
            'username'  : o['usr'],
            'password'  : o['pwd'],
            'db'        : o['db'] if 'db' in o and o['db'] and o['db'] != '' else 0,
            'decode_responses' : True,
        }
        try:
            self.dbcfg['db'] = int(o['db'])
        except (AttributeError, ValueError):
            self.dbcfg['db'] = 0

        self.dbcfg.update(kwargs)
        self._conn = Redis(connection_pool=BlockingConnectionPool(**self.dbcfg))
    
    def close(self):
        self._conn.connection_pool.disconnect()

    def set(self, key, value):
        rtn = None
        if isinstance(value, str):
            rtn = self._conn.set(key, value)
        elif isinstance(value, dict):
            rtn = self._conn.hmset(key, value)
        elif isinstance(value, list):
            self._conn.delete(key)
            rtn = self._conn.rpush(key, *value)
        elif isinstance(value, set):
            self._conn.delete(key)
            rtn = self._conn.sadd(key, *value)
        else:
            rtn = self._conn.set(key, value)
        return rtn

    def get(self, key, data_type=None):
        rtn = None
        t = data_type if data_type is not None else self._conn.type(key)
        if t == 'string':
            rtn = self._conn.get(key)
        elif t == 'hash':
            rtn = self._conn.hgetall(key)
        elif t == 'list':
            rtn = self._conn.lrange(key, 0, -1)
        elif t == 'set':
            rtn = self._conn.smembers(key)
        else:
            rtn = self._conn.get(key)
        return rtn

    def setby(self, key, field, value, data_type=None):
        '''key: key(hash) or index(list)
        return None if not support
        '''
        rtn = None
        t = data_type if data_type is not None else self._conn.type(key)
        if t == 'hash':
            rtn = self._conn.hset(key, field, value)
        elif t == 'list':
            rtn = self._conn.lset(key, field, value)
        return rtn
    
    def getby(self, key, field, data_type=None):
        '''key: key(hash) or index(list)
        return None if not support
        '''
        rtn = None
        t = data_type if data_type is not None else self._conn.type(key)
        if t == 'hash':
            rtn = self._conn.hget(key, field)
        elif t == 'list':
            rtn = self._conn.lindex(key, field)
        return rtn
    
    def delby(self, key, fields, data_type=None):
        '''fields: fields(hash) or indexes(list) or values(set)
        return None if not support
        '''
        rtn = None
        t = data_type if data_type is not None else self._conn.type(key)
        if t == 'hash':
            rtn = self._conn.hdel(key, *fields)
        elif t == 'list':
            with self._conn.pipeline() as p:
                p.multi()
                for idx in fields:
                    p.lset(key, idx, '__ZWREDIS_DELETED__')
                rtn = p.lrem(key, 0, '__ZWREDIS_DELETED__')
                p.execute()
        elif t == 'set':
            self._conn.srem(key, *fields)
        return rtn
    
    def append(self, key, value, data_type=None):
        rtn = None
        t = data_type if data_type is not None else self._conn.type(key)
        if t == 'string':
            rtn = self._conn.append(key, value)
        elif t == 'hash':
            rtn = self._conn.hmset(key, value)
        elif t == 'list':
            rtn = self._conn.rpush(key, *value)
        elif t == 'set':
            rtn = self._conn.sadd(key, *value)
        else:
            rtn = self._conn.append(key, value)
        return rtn
    
    def contains(self, key, field, data_type=None):
        '''key: key(hash) or value(list/set) or substring(string)'''
        rtn = None
        t = data_type if data_type is not None else self._conn.type(key)
        if t == 'string':
            rtn = field in self._conn.get(field)
        elif t == 'hash':
            rtn = self._conn.hexists(key, field)
        elif t == 'list':
            with self._conn.pipeline() as p:
                p.multi()
                set_name = '_%s_tmp_set' % key
                p.delete(set_name)
                arr = self._conn.lrange(key, 0, -1)
                p.sadd(set_name, *arr)
                p.sismember(set_name, field)
                p.delete(set_name)
                rtn = p.execute()
                rtn = rtn[2]
        elif t == 'set':
            rtn = self._conn.sismember(key, field)
        else:
            rtn = field in self._conn.get(field)
        return rtn
    
    def len(self, key, data_type=None):
        rtn = None
        t = data_type if data_type is not None else self._conn.type(key)
        if t == 'string':
            rtn = self._conn.strlen(key)
        elif t == 'hash':
            rtn = self._conn.hlen(key)
        elif t == 'list':
            rtn = self._conn.llen(key)
        elif t == 'set':
            rtn = self._conn.scard(key)
        else:
            rtn = self._conn.strlen(key)
        return rtn
    
    def all(self):
        rtn = []
        keys = self._conn.keys('*')
        for key in keys:
            rtn.append({
                'key': key,
                'value': self.get(key)
            })
        return rtn
    
    def all_iter(self, cbfunc):
        for key in self._conn.scan_iter():
            cbfunc(key)

    def delete(self, name):
        return self._conn.delete(name)

    def exists(self, key):
        return self._conn.exists(key) == 1

    def dbsize(self):
        return self._conn.dbsize()

    @property
    def conn(self):
        return self._conn

    def __repr__(self):
        return '<Database host={}:{}>'.format(self.dbcfg['host'], self.dbcfg['port'])

    def __enter__(self):
        return self

    def __exit__(self, exc, val, traceback):
        self.close()
Example #14
0
    # 键均不存在时才批量赋值
    mapping = dict(uxs1='vs1', uxs2='vs2')
    print rc.msetnx(mapping)
    print rc.mget('uxs1', 'uxs2')
    print DIVIDING

    # 键为name的value增值操作,默认为1,键不存在则被创建并设为amount
    rc.set('num', 1)
    print rc.incr('num', amount=2)
    print rc.incrby('num', amount=3)

    # 键为name的value减值操作,默认为1,键不存在则被创建并设为-amount
    print rc.decr('num', amount=3)
    print rc.decrby('num', amount=3)
    print DIVIDING

    # 键为name的string的值追加value
    rc.set('append', 'asd')
    print rc.append('append', 'asdasdasd')
    print rc.get('append')
    print DIVIDING

    # 返回键为name的string的子串
    print rc.substr('append', 0, 5)

    # 获取键的value值从start到end的子字符串
    print rc.getrange('append', 0, 5)

    rc.flushdb()
    rc.close()