问题
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