Ejemplo n.º 1
0
def index():
    notes = None
    if current_user.is_authenticated:
        if not redis_client.exists(f'{current_user.id}:notes_id'):
            notes = current_user.notes.order_by(Note.edit_date.desc())
            if notes.count() != 0:
                for note in notes:
                    note_dict = NoteConverter.note_to_dict(note)
                    redis_client.lpush(f'{current_user.id}:notes_id',
                                       note_dict['id'])
                    redis_client.hmset(note_dict['id'], note_dict)
                flash('Your notes where loaded from postgresql !', 'info')
            else:
                notes = None
        else:
            notes = []
            count = 0
            while count < redis_client.llen(f'{current_user.id}:notes_id'):
                note_id = redis_client.lindex(f'{current_user.id}:notes_id',
                                              count)
                notes.append(
                    NoteConverter.dict_to_note(redis_client.hgetall(note_id)))
                count += 1
            flash('Your notes where loaded from redis !', 'info')
    return render_template("index.html", title='Your notes', notes=notes)
Ejemplo n.º 2
0
def limit_handler():
    # return: True: 允许; False: 拒绝
    amount_limit = 100  # 限制数量
    key_name = 'xxx_goods_limit'  # redis key name
    incr_amount = 1  # 每次增加数量
    # 判断key是否存在
    if not redis_client.exists(key_name):
        # setnx是原子性的,允许并发操作
        redis_client.setnx(key_name, 100)
    # 数据插入后再判断是否大于限制数
    if redis_client.incrby(key_name, incr_amount) <= amount_limit:
        return True
    return False
def attack():
    if not request.json or not 'user' in request.json or not 'password' in request.json or not 'metadata' in request.json:
        abort(400)
    try:
        metadata = request.json['metadata']
        ip = metadata['IP']
        if(redis_client.exists(ip)):
            data = redis_client.get(ip)
            ip_stats = json.loads(data)
            td = datetime.now() - datetime.strptime(ip_stats['last_time'], '%Y-%m-%d %H:%M:%S.%f')
            if (int(td.total_seconds()) >= 1):
                ip_stats['last_time'] = datetime.now()
                ip_stats['count'] = 0
            if(ip_stats['count'] <= 5):
                ip_stats['count'] = ip_stats['count'] + 1
            if(ip_stats['count'] > 5 and int(td.total_seconds()) < 1):
                return jsonify({"Authentication": "Blocked"}), 201
            rval = json.dumps(ip_stats, default=myconverter)
            redis_client.set(ip, rval)
        else:
            ip_stats = {}
            ip_stats['count'] = 1
            ip_stats['last_time'] = datetime.now()
            rval = json.dumps(ip_stats, default = myconverter)
            redis_client.set(ip, rval)
        with sql.connect(DATABASE) as con:
            cur = con.cursor()
            user = request.json['user']
            password = request.json['password']
            cur.execute("SELECT rowid from USER where name = ? and password = ?", (user, password))
            data = cur.fetchone()
            if data is not None:
                return jsonify({"Authentication": True}), 200
    except Exception as e:
        print(e)
    return jsonify({"Authentication": False}), 200
Ejemplo n.º 4
0
    def generate_verification_code():
        while True:
            temp_code = (random_string_generator.generate_verification_code())

            if not (redis_client.exists(temp_code)):
                return temp_code
Ejemplo n.º 5
0
def counter_get():
    if not r.exists("counter"):
        r.set('counter', 0)
    return jsonify(int(r.get("counter")))
Ejemplo n.º 6
0
def seen_get():
    if not r.exists("seen"):
        r.set('seen', 0)
    return jsonify(int(r.get("seen")))