python-2.7 - 来自 Chrome 的 Http 请求挂起 python 网络服务器

标签 python-2.7 python-3.x google-chrome pyopenssl

我有一个非常基本的 python (2.7.12) web 服务器(我已经尽可能地将它剥离了),下面给出的代码

import time
import ssl
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

class Management(SimpleHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        SimpleHTTPRequestHandler.end_headers(self)
        self.wfile.write(time.asctime() + "\n")

httpd = HTTPServer(('', 4443), Management)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./server.pem', server_side=True)
httpd.serve_forever()

它所做的只是返回时间。当我通过 https 访问它时它工作正常,但是当我在另一台计算机上使用(最新版本的)chrome 通过 http 访问它时,它经常(但不总是)导致整个服务器卡在 python 代码中,特别是 ssl .py 在 do_handshake 函数中的行
self._sslobj.do_handshake()

我希望连接失败并被删除,因为我试图通过 http 访问 https 页面,但我不希望它导致整个过程挂起。它只发生在 chrome(不是 firefox 或 microsoft edge)上,并且仅当 chrome 在与运行服务器的计算机不同的计算机上运行时才会发生。

我也试过创建代码的 python3 版本,我看到了同样的问题。我已经尝试在 Windows 10 上的 Cygwin 和 Ubuntu 14.04 中的终端中运行它,但我遇到了同样的问题。

最佳答案

刚刚完成了与此搏斗的 2 天:(
事实证明,用户 Ami Bar 的回答完全正确。谷歌浏览器保持打开连接并导致选择器库中的多路复用将连接注册为可读。从本质上讲,它会强制多路复用失败,因此您必须将服务器线程化以单独处理请求。
您可以通过实现 socketserver.ThreadingMixIn 来线程化您的 HTTP 服务器。 .但是,如果它更容易(这就是我所做的),您可以简单地将 python 升级到 3.7+ 并让您的服务器继承自 http.server.ThreadingHTTPServer , native 实现 ThreadingMixIn希望这能拯救一些可怜的灵魂两天......

关于python-2.7 - 来自 Chrome 的 Http 请求挂起 python 网络服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43146298/

相关文章:

python - 如何在 python 中执行 excel 的 'format as table'

google-chrome - Chrome 扩展本地存储 : Background. html 无权访问本地存储?

javascript - 加载 Angular 应用程序时,Chrome devtools 挂起

python - 使用 selenium webdriver 登录网站时出错

python - Python中的过滤操作

python - 在 Python 中对混合列表进行排序

python-2.7 - 编写递归函数来查找 Hermite 多项式

javascript - 尝试使用 CustomJS 加载新图像时 Bokeh 查找临时文件

c# - 如何将使用 dotnet-core 构建的 DLL 与 Python 集成

python - pymongo 在文档中更新/插入字典