def webtest():
# 每次执行请求读取数据库获取变量值
sr = StrictRedis()
start_time = sr.hget('access', 'start_time')
get_count = sr.hget('access', 'get_count')
post_count = sr.hget('access', 'post_count')
# 如果为空,则为首次运行,设置开始时间
if not start_time:
start_time = int(time.time())
sr.hset('access', 'start_time', start_time)
# 不为空判断是否达到预定时间间隔
else:
# 如果达到将数据库中的数据写入本地文件,并将计数清零,开始时间设置为当前时间
if (int(time.time()) - int(start_time)) / 60 > 5:
end_time = datetime.now()
print('结束时间:%s,5分钟内post请求数量%d,get请求数量%d' % (str(end_time), int(post_count), int(get_count)))
with open('access_log.txt', 'a') as f:
f.write('结束时间:%s 5分钟内post请求数量%d get请求数量%d ' % (str(end_time), int(post_count), int(get_count)))
start_time = int(time.time())
sr.hset('access', 'start_time', start_time)
get_count = 0
post_count = 0
sr.hset('access', 'get_count', get_count)
sr.hset('access', 'post_count', post_count)
if request.method == 'POST':
# 如果是post请求,判断是否计数为空,如果为空,则为首次运行,将值设置为1
if not post_count:
sr.hset('access', 'post_count', 1)
else:
# 如果不为空,则值加1
sr.hset('access', 'post_count', int(post_count) + 1)
return jsonify({"message": "post ok"})
if not get_count:
# 判断get请求计数是否为空,如果为空,则为首次运行,将值设置为1
sr.hset('access', 'get_count', 1)
else:
# 如果不为空,则值加1
sr.hset('access', 'get_count', int(get_count) + 1)
return jsonify({"message": "get ok"})