python - flask -socket.error : [Errno 10053] An established connection was aborted by the software in your host machine

标签 python sockets flask

<分区>

根据要求重新打开这个问题(error: [Errno 10053]),提供最小的可测试示例:

import time
from flask import Flask, render_template
app = Flask(__name__, static_folder='static', template_folder='templates')

@app.route('/')
def main():
    return render_template('test.html')

@app.route('/test')
def test():
    print "Sleeping. Hit Stop button in browser now"
    time.sleep(10)
    print "Woke up. You should see a stack trace from the problematic exception below."
    return render_template('test.html')

if __name__ == '__main__':
    app.run()

HTML:

<html>
<body>
<a href="/test">test</a>
</body>
</html>

指南: 运行应用程序,导航到 localhost:port,单击链接,然后单击浏览器中的停止按钮。 sleep 结束后,您应该会看到异常。 sleep 是模拟服务器上发生的任何类型事件所必需的。这可能只是几秒钟:如果用户设法离开页面 - Flask 将崩溃。

socket.error: [Errno 10053] An established connection was aborted by the software in your host machine

为什么服务器停止为应用程序提供服务?我可以为我的 Flask 应用程序使用什么其他服务器来避免这种情况?

最佳答案

这是 SocketServer 模块的 Python 2 实现的问题,它不存在于 Python 3(服务器继续提供服务)中。

您有 3 个选择:

  • 不要将内置服务器用于生产系统(毕竟它是一个开发 服务器)。使用合适的 WSGI 服务器,如 gunicornuWSGI ,
  • 使用 app.run(threaded=True) 启用线程模式;线程死了,但为将来的请求创建了一个新线程,
  • 升级到 Python 3。

关于python - flask -socket.error : [Errno 10053] An established connection was aborted by the software in your host machine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40247025/

相关文章:

java - AppInventor2 TCP 客户端扩展使应用程序崩溃

android - 在没有root的linux/android上为UDP数据包分配传出接口(interface)

python - Flask 蓝图模板的最佳布局?

ubuntu - 在 Ubuntu+Nginx 上使用 Gunicorn 部署 Dash 应用程序

python - Reportlab - 将两个段落添加到一个表格单元格中

python - 在 Keras 中使用和不使用 Sequential() 构建模型有什么区别?

java - Android 上的异步套接字 I/O

flask - 在 gunicorn 中运行的 Plotly Dash 中获取请求头

python - 是否可以单独使用 Django 管理创建面向最终用户的站点?

python - 如何在 Python 中获取键码