zoukankan      html  css  js  c++  java
  • flask+redis获取一定时间段内用户的get请求和post请求的数量

    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"})
  • 相关阅读:
    iot 表索引dump《2》
    heap表和iot表排序规则不同
    Cannot complete the install because one or more required items could not be found.
    iot表输出按主键列排序,heap表不是
    iot 表主键存放所有数据,且按数据插入顺序排序
    iot表和heap表排序规则不同
    org.eclipse.graphiti.ui.editor.DiagramEditorInput.
    Oracle 排序规则
    perl 异步超时 打印错误
    14.6.3 Grouping DML Operations with Transactions 组DML操作
  • 原文地址:https://www.cnblogs.com/sunmingduo/p/10232190.html
Copyright © 2011-2022 走看看