def before_request(): #flask sessions expire once you close the browser unless you have a permanent session session.permanent = True #By default in Flask, permanent_session_lifetime is set to 31 days. app.permanent_session_lifetime = timedelta(minutes=30) max_login_retry = 5 ip = request.headers['X-Real-IP'] or request.headers['Remote_Addr'] if (not getattr(g.identity, 'user', None)) and request.path == url_for_security('login'): if request.method == 'POST' and request.form['password']: login_retry = session.get('login_retry', 0) + 1 session['login_retry'] = login_retry #使用redis来限制ip登录限制 key = session_ip_prefix + ip with redis.pipeline() as pipe: pipe.incr(key).expire(key, 2 * 3600).execute() #print(type(redis.get(key))) ip_retry_count = int(redis.get(key)) if login_retry > max_login_retry or ip_retry_count > max_login_retry: return render_template('hintInfo.html', msg='登录次数超出限制,请2小时后重试') log.info(ip + " enter into " + request.path or '' + " for " + request.endpoint or '' + " of http method:" + request.method)
def get_post_content(abspath): from .utilRedis import redis_client as redis key = 'post_get:%s' % abspath if redis.exists(key): alldict = redis.hgetall(key) html = alldict[b'html'].decode('utf-8') toc = alldict[b'toc'].decode('utf-8') meta = json.loads(alldict[b'meta'].decode('utf-8'), encoding='utf-8') redis.expire(key, 60 * 60 * 1) return html, toc, meta with open(abspath, encoding='UTF-8') as f: content = f.read() # title=content.split('\n\n',1)[0] # content=content.split('\n\n',1)[1] md_ext = Constant.md_ext md = markdown.Markdown(output_format='html5', encoding='utf-8', extensions=md_ext) html = html_clean(md.convert(content)) toc = md.toc or '' meta = md.Meta or {} with redis.pipeline() as pipe: pipe.hmset(key, { 'html': html, 'toc': toc, 'meta': json.dumps(meta) }).expire(key, 60 * 60 * 1).execute() return html, toc, meta
def releasePostLock(path): key0, key1 = getPostLockKey(path) with redis.pipeline() as pipe: pipe.delete(key0, key1).execute()
def createPostLock(path): key0, key1 = getPostLockKey(path) with redis.pipeline() as pipe: pipe.setex(key0, 30 * 60, 1).setex(key1, 60, current_user.email).execute()