Flask Snippets

Snippets are unofficial and unmaintained.

This is an archived view of user-submitted snippets. Despite being hosted on the Flask site, they are not official. No Flask maintainer has curated or checked the snippets for security, correctness, or design.

Shutdown The Simple Server

Posted by Armin Ronacher on 2011-08-29 @ 10:14 and filed in Utilities

The Werkzeug server that is used by the app.run() command can be shut down starting with Werkzeug 0.8. This can be helpful for small applications that should serve as a frontend to a simple library on a user's computer.

from flask import request

def shutdown_server():
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug Server')

Now you can shutdown the server by calling this function:

@app.route('/shutdown', methods=['POST'])
def shutdown():
    return 'Server shutting down...'

The shutdown functionality is written in a way that the server will finish handling the current request and then stop.

This snippet by Armin Ronacher can be used freely for anything you like. Consider it public domain.