python - 未能结束 Flask 应用程序

标签 python flask internal-server-error

我有一个 flask 应用程序如下:

socketio2.run(app2, host="0.0.0.0", port=4998)

我创建了一个方法来结束它,如下所示:

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...'

我收到以下错误

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.

我的目标是结束这个 Flask 应用程序并随后运行 Python 脚本。有什么建议吗?

最佳答案

当您将服务器作为套接字服务器运行时,默认关闭服务器代码将不起作用。但是,socketio 确实提供了一个停止的包装器,当您使用它时,默认行为应该停止服务器。 https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/flask_socketio/init.py#L510

因此,如果您将代码修改为类似这样的内容 -

def shutdown_server():
    socketio2.stop()

然后它应该完成这项工作

关于python - 未能结束 Flask 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40807784/

相关文章:

perl - Perl 脚本抛出内部服务器错误 500 - 但脚本工作正常

python - plt.tight_layout 带有长标题的错误?

python - 如何统计一个函数被调用的次数?

python - Django Wagtail Page.objects.descendant_of(inclusive=False) 错误

python - Flask-Security token 是如何创建的?

docker - Kubernetes 如何调用 Docker 镜像?

node.js - heroku sails 部署上的内部服务器错误

python - Python 中的 Swift Optionals(或 Haskell 的 Maybe)?

关闭腻子后 Flask 应用程序未运行

python - "Internal Server Error"与 Hello World Python 应用程序