Flask - detect disconnection

元气小坏坏 提交于 2021-02-08 08:43:22

问题


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).


回答1:


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.



来源:https://stackoverflow.com/questions/26364732/flask-detect-disconnection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!