烧瓶-检测断开连接 [英] Flask - detect disconnection

查看:51
本文介绍了烧瓶-检测断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检测用户何时关闭/刷新网络?我的程序如下所示:

  @ app.route('/')def form():返回render_template('form_ConfusionSet_submit.html')@ app.route('/hello/',methods = ['GET','POST'])def hello():一些行动...返回render_template('form_action.html',text = text)@ app.route('/add/',methods = ['POST'])def add():一些行动...返回render_template('form_text_submit.html',USER = USER) 

我需要在用户退出时从服务器中删除一些文件(无论在哪一步).

解决方案

您可以使用 websockets .在Flask中,有一些软件包可以完成此操作,但是这里是 Flask-SocketIO ./p>

在GitHub存储库上的示例中, socketio.on('connect')装饰器用于客户端每次连接到后端时都会调用的方法. socketio.on('disconnect')从后端断开连接时,都会调用code>装饰器.

使用这些,您可以在客户端连接和断开服务时执行操作.

Is there any way to detect when the user close/refresh the web? My program look something like this:

@app.route('/')
def form():
    return render_template('form_ConfusionSet_submit.html')
@app.route('/hello/', methods=['GET','POST'])
def hello():
    some action...
    return render_template('form_action.html', text=text)

@app.route('/add/', methods=['POST'])
def add():
    some action...
    return render_template('form_text_submit.html',USER = USER)

I need to delete some files from the server when the user exit(no matter in whice step).

解决方案

You can do this with websockets. There are a few packages to accomplish this in Flask, but here's Flask-SocketIO.

From the example on the GitHub repo, the socketio.on('connect') decorator is used on a method that is called whenever a client connects to the backend. socketio.on('disconnect') decorator is called whenever a client disconnects from the backend.

Using these, you can perform actions when a client connects and disconnects from your service.

这篇关于烧瓶-检测断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆