I have a flask application as follows:
socketio2.run(app2, host="0.0.0.0", port=4998)
I created a method to end it as follows:
def shutdown_server():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
@app2.route('/shutdown', methods=['POST'])
def shutdown():
shutdown_server()
print " shutdown"
return 'Server shutting down...'
I get the following error
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
My target is to end this Flask app and run a Python script afterwards. Any suggestions ?
As you are running the server as a socketio server, the default shutdown server code will not work. However, socketio does provide a wrapper to stop and when you use it, the default behaviour should stop the server. https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/flask_socketio/init.py#L510
So if you modify your code to something like this -
def shutdown_server():
socketio2.stop()
Then it should do the job