title: Prevent Flask OSError: [Errno 98] Address already in use
body: <p>I have a Flask app, which is called via putty console and runs on port 5000. It is only supposed to run temprarily, there will always be only one user using the app. Every now and then, I get the &quot;address already in use&quot; error, which can be solved by &quot;kill -9 PID&quot;, where PID is the id of the flask app.</p>
  <p>Is there a behaviour of the user that usually causes this error? Until now it seems kind of random to me. Also, what can be done to prevent this error?</p>
  <p>Right now, I either shut the app with crtl+c or with an &quot;exit&quot;-button on the page which leads to this page:</p>
  <pre><code>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('/exit', methods=[&quot;POST&quot;, &quot;GET&quot;])
  def shutdown():
      if request.method == &quot;POST&quot;:
          shutdown_server()
          return render_template('exit.html', app_data=app_data, )
  
  </code></pre>
  <p>&quot;exit.html&quot; just asks the user to close the window.
  This button was already an attempt to prevent an unhealthy shutdown.</p>
  <p>I also thought of a dynamic change of the port, but this will make it hard to start it via putty.</p>
  <p>I am looking for a solution, that is intuitive, to prevent a non-coder user from being forced to kill the process himself. Thanks for your help!</p>