ConnectionResetError: [WinError 10054] 现有连接被 ChromeDriver Chrome Selenium Django 的远程主机错误强行关闭

Posted

技术标签:

【中文标题】ConnectionResetError: [WinError 10054] 现有连接被 ChromeDriver Chrome Selenium Django 的远程主机错误强行关闭【英文标题】:ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host error with ChromeDriver Chrome Selenium Django 【发布时间】:2020-04-25 05:37:35 【问题描述】:

我正在通过“Obey the testing goat book”学习 TDD,但我正在尝试使用 Django 3 来学习。

如果有人知道,我在第 6 章。

我的代码是:

class VisitorTest(LiveServerTestCase):

    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.implicitly_wait(2)

    def tearDown(self):
        self.browser.quit()

    def test_starting(self):
        print(self.live_server_url) 

        self.browser.get(self.live_server_url)

在控制台中我得到了


Creating test database for alias 'default'...
System check identified no issues (0 silenced).

DevTools listening on ws://127.0.0.1:52187/devtools/browser/e9a03a04-819e-40a3-a0e4-bd4133d8f6cb
http://localhost:52180
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52204)
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52202)
Exception happened during processing of request from ('127.0.0.1', 52203)
Traceback (most recent call last):
Traceback (most recent call last):
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 654, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 654, in process_request_thread
    self.finish_request(request, client_address)
Traceback (most recent call last):
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 724, in __init__
    self.handle()
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 724, in __init__
    self.handle()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 172, in handle
    self.handle_one_request()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 172, in handle
    self.handle_one_request()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
----------------------------------------
----------------------------------------
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 654, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 724, in __init__
    self.handle()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 174, in handle
    self.handle_one_request()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
----------------------------------------
.
----------------------------------------------------------------------
Ran 1 test in 8.779s

OK
Destroying test database for alias 'default'...

但是测试运行良好(我没有在这里展示它们)。

我进行了研究,但没有发现任何结论

知道为什么要显示这些以及如何修复它们吗?

【问题讨论】:

【参考方案1】:

我尝试了不同的建议解决方案,但均未成功:

changing test server address 从“本地主机”到“127.0.0.1” 更新旧的 ChromeDriver,因为 Anaconda 的 ChromeDriver 2.38 未正式支持我的 Chrome 83 在调用quit() 之前添加self.browser.refresh()(作为书本身suggest) 使用Edge浏览器(嗯……IE驱动没有这个问题,我也没试过FF)

如果我们深入研究,这种错误由LiveServerTestCase.server_thread.httpd.handle_error 函数处理,socketserver.BaseServer 中的默认实现只是将错误消息打印到stderr。由于这些消息是无害的,我决定将它们隐藏起来。

import sys
from contextlib import contextmanager

@contextmanager
def suppress_stderr():
    "Temporarly suppress writes to stderr"
    class Null:
        write = lambda *args: None
    err, sys.stderr = sys.stderr, Null
    try:
        yield
    finally:
        sys.stderr = err

# Suppress stderr messages during quit process
with suppress_stderr():
    self.browser.quit()

【讨论】:

【参考方案2】:

此错误消息...

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

...暗示 ChromeDriverBrowsing Context 之间的连接,即 Chrome 浏览器 会话间歇性关闭。

当您使用的二进制文件版本之间不兼容时,会出现此问题。


解决方案

确保:

Selenium 升级到当前级别 Version 3.141.59。 ChromeDriver 已更新到当前的ChromeDriver v79.0.3945.36 级别。 Chrome 已更新至当前 Chrome 版本 79.0 级别。 (根据ChromeDriver v79.0 release notes) 清理你的项目工作区通过你的IDE重建你的项目只需要依赖。 (仅限 Windows 操作系统)使用 CCleaner 工具在执行您的测试套件之前和之后清除所有操作系统琐事。 (仅限 LinuxOS)Free Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint 在执行您的测试套件之前和之后。 如果您的基本 Web 客户端 版本太旧,请卸载它并安装最新的 GA 和发布版本的 Web 客户端。 进行系统重启。 以非root用户身份执行@Test。 始终在 tearDown() 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例。

参考

您可以在以下位置找到一些相关讨论:

urllib3.exceptions.ProtocolError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host'))

【讨论】:

【参考方案3】:

如果你正在使用 run_subprocess() 设置 detach_mode=True 并尝试一下

【讨论】:

正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于ConnectionResetError: [WinError 10054] 现有连接被 ChromeDriver Chrome Selenium Django 的远程主机错误强行关闭的主要内容,如果未能解决你的问题,请参考以下文章

ConnectionResetError: [WinError 10054] 现有连接被 ChromeDriver Chrome Selenium Django 的远程主机错误强行关闭

会引发ConnectionResetError的代码

Could not install packages due to an OSError: ("Connection broken: ConnectionResetError(10054,

解决 ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')) 问题(

python3+selenium 3.13 + geckodriver 21.0,提示ConnectionResetError,切换会较低版本的driver即可

Django错误: ConnectionResetError: [Errno 54] Connection reset by peer