I am using a flask server. I want to stop a server by script.
Below is my code, but for stopping a server, I have to manually stop it. I want that after 5 seconds the server is automatically stopped.
How can I do that?
import sys
from flask import Flask, send_file, request, jsonify
import os
upload_directory="/home/einfochips/Desktop/"
file_name = "android.tar.gz"
app = Flask(__name__)
if not os.path.exists(upload_directory):
sys.exit(0)
@app.route('/')
def list_files():
files = []
for filename in os.listdir(upload_directory):
path = os.path.join(upload_directory,filename)
if os.path.isfile(path):
files.append(filename)
return jsonify(files)
def shutdown_server():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
@app.route('/shutdown')
def shutdown():
shutdown_server()
return 'Server shutting down...'
if __name__ == '__main__':
app.run(debug = True)