【问题标题】:Flask - socket.error: [Errno 10053] An established connection was aborted by the software in your host machine [duplicate]Flask - socket.error:[Errno 10053]已建立的连接被主机中的软件中止[重复]
【发布时间】:2016-10-25 18:21:50
【问题描述】:

应要求重新打开此问题 (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,单击链接,然后点击浏览器中的停止按钮。睡眠结束后,您应该会看到异常。 睡眠对于模拟服务器上发生的任何类型的活动是必要的。可能只有几秒钟:如果用户设法离开页面 - Flask 将会崩溃。

socket.error: [Errno 10053] 已建立的连接被 主机中的软件

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

【问题讨论】:

  • 如果您通过推荐的方式之一(即 apache 或 nginx)提供它,这可能根本不会成为问题 ...
  • 我之前用完全相同的答案回答了一个类似的问题,所以我复制了这个(另一个问题是关于并发请求但停止curl 然后导致管道回溯中断)。

标签: python sockets flask


【解决方案1】:

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

您有 3 个选择:

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

【讨论】:

  • 确认:升级到 Python3,问题不再存在。谢谢!
  • 我在我的虚拟环境中使用 Python3,但我仍然在我的 Django 应用程序中得到它。
  • @CoolCK: 没有minimal reproducible example 我帮不了你,对不起。
猜你喜欢
  • 2015-06-25
  • 2019-01-04
  • 2021-02-02
  • 2019-06-25
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多