简体   繁体   English

Flask 长轮询 API

[英]Flask Long Polling API

I am trying to get a webpage to update from an API endpoint whenever that endpoint updates.每当端点更新时,我都试图从 API 端点获取要更新的网页。 Right now my webpage works if I refresh it but I want it to update automatically.现在我的网页如果我刷新它就可以工作,但我希望它自动更新。

import json
import requests
from flask import Flask, render_template


app = Flask(__name__)
s = requests.session()

# API Key
s.headers.update({'Authorization': 'apikeygoeshere'})


@app.route("/")
def index():
    alarms = s.get('https://site/api/alarms')
    alarmData = json.loads(alarms.text)

    if next in alarmData:
        while alarmData['next']:
            nextUrl = alarmData['next']['href']
            print(nextUrl)
            nextUrlGet = s.get(nextUrl)
            nextData = json.loads(nextUrlGet.text)
            alarmData = nextData

            if not alarmData['next']:
                break
    else:
            print('No Next -> Update Ready')
            alarmUpdateUrl = alarmData['updates']['href']
            update = s.get(alarmUpdateUrl)
            updateData = json.loads(update.text)

            if not updateData['updates']:
                updateName = "Nothing"

            else:
                updateName = updateData['updates'][0]['source']['name']
                alarmUpdateUrl = updateData['next']['href']
                print(alarmUpdateUrl)
                s.get(alarmUpdateUrl)
                
            return render_template('index.html', data=updateName, url=alarmUpdateUrl)



if __name__ == '__main__':
    app.run(debug=True, threaded=True)

I tried putting the code in a while loop but it didn't seem to restart.我尝试将代码放在一个while循环中,但它似乎没有重新启动。 Esentially the endpoint may not have an updates url on page one so i have to follow the next link until there is an update href then request that to initiate a long poll.本质上,端点可能在第一页上没有更新 url,所以我必须点击下一个链接,直到有更新 href 然后请求启动长轮询。

When you call an endpoint, Flask figures out which method to run, runs this method, and returns a response, in your case HTML.当您调用端点时,Flask 会确定要运行的方法,运行此方法并返回响应,在您的情况下为 HTML。 The connection between client (your browser) and server is then terminated and flask stops doing anything (more or less) until a new request comes in. If you want a website to refresh automatically, you have a few options, but all of them have to be implemented on the client side:然后客户端(您的浏览器)和服务器之间的连接被终止,并且 flask 停止做任何事情(或多或少),直到有新请求进来。如果您希望网站自动刷新,您有几个选项,但它们都有在客户端实现:

  1. Add <meta http-equiv="refresh" content="30"> to your html head in index html.<meta http-equiv="refresh" content="30">添加到索引 html 中的 html 头部。 This will cause your browser to reload the page every 30 seconds.这将导致您的浏览器每 30 秒重新加载一次页面。
  2. Add some javascript to your index.html which will poll your API endpoint and update your page when new information is available.添加一些 javascript 到您的 index.html 它将轮询您的 API 端点并在新信息可用时更新您的页面。
  3. Use websockets (this is similar to the pure javascript approach but potentially with less polling).使用 websockets(这类似于纯 javascript 方法,但可能会减少轮询)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM