bootstrap 图片轮询_轮询、长轮询和websocket

一、轮询

在一些需要进行实时查询的场景下应用

比如投票系统:

大家一起在一个页面上投票

在不刷新页面的情况下,实时查看投票结果

1、后端代码

from flask importFlask, render_template, request, jsonify

app= Flask(__name__)

USERS={1: {'name': '小米', 'count': 300},2: {'name': '小康', 'count': 200},3: {'name': '小明', 'count': 600},

}

@app.route('/')defindex():return render_template('Poll.html', users=USERS)

@app.route('/vote', methods=['POST'])defvote():#接收uid,通过uid给票数 +1

#用户提交Json数据过来,用request.json获取

uid = request.json.get('uid')

USERS[uid]['count'] += 1

return "投票成功"@app.route('/get_vote')defget_vote():#返回users数据

#jsonify 是flask自带的序列化器

returnjsonify(USERS)if __name__ == '__main__':

app.run()

2、前端代码

投票系统

}

最帅男人投票

{% for (uid, user) in users.items() %}投票

{ { user.name }}目前的票数是: { { user.count }}{% endfor %}

你可能感兴趣的:(bootstrap,图片轮询)