且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ConnectionResetError:[Errno 104]由对等端和ERR_NAME_NOT_RESOLVED在heroku上重置连接,并通过Selenium进行了移动测试

更新时间:2021-10-31 21:44:16

ConnectionResetError:[Errno 104]对等重置连接

通常,当客户端突然终止而不关闭连接时,基础操作系统的 TCP/IP 堆栈会发送RST packet. Python将其转换为带有文本由对等方重置的连接的异常.根据您的错误堆栈跟踪,这意味着一旦(内部)调用self._read_status() Python 就假定已接收到某些东西,但连接突然断开,而 Python 通过引发异常来通知您该错误:

ConnectionResetError: [Errno 104] Connection reset by peer

In general when a client terminates abruptly without closing the connection a RST packet is sent by the TCP/IP stack of the underlying OS. Python converts this into an exception with the text Connection reset by peer. As per your error stack trace it means that once self._read_status() was invoked (internally) Python assumed to receive something but the connection was suddenly dropped and Python informs you of this error by raising the exception:

ConnectionResetError: [Errno 104] Connection reset by peer

这种情况有点类似于此表达式:

由对等方重置连接"是TCP/IP的等效功能,它相当于将电话重新挂到挂机上.礼貌比不回信,挂个电话更礼貌.但这不是真正礼貌的TCP/IP转换程序的FIN-ACK.

"Connection reset by peer" is the TCP/IP equivalent of slamming the phone back on the hook. It's more polite than merely not replying, leaving one hanging. But it's not the FIN-ACK expected of the truly polite TCP/IP converseur.

此错误背后可能有多种概率,如下所示.

There can be multiple probabilities behind this error as follows.

一种快速而精确的解决方案是,添加一些建议的 ChromeOptions 以及现有的以下选项:

A quick and a precise solution will be to add a few recommended ChromeOptions along with the existing one as follows:

options.add_argument("start-maximized"); // open Browser in maximized mode
options.add_argument("disable-infobars"); // disabling infobars
options.add_argument("--disable-extensions"); // disabling extensions
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage"); // overcome limited resource problems

然后

return webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=options) 

注意:您需要删除参数options.add_argument('--disable-gpu'),因为它仅适用于Windows操作系统.

Note: You need to remove the argument options.add_argument('--disable-gpu') as it is applicable to windows OS only.

几点:

  • 根据 Python Chrome Mobile Emulation 中的文档>键和对似乎是 "deviceName": "Google Nexus 5" (不是"deviceName": "Nexus 5")
  • 您可以通过以下两种方法之一来调整代码以调用Remote()-

  • As per the documentation within Python Chrome Mobile Emulation the Key and Value pair seems to be "deviceName": "Google Nexus 5" (not "deviceName": "Nexus 5")
  • You can tweak you code to invoke Remote() through either of the following ways-

  • 通过DesiredCapabilities()调用Remote():

from selenium import webdriver
# import DesiredCapabilities was missing in your program
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

mobile_emulation = { "deviceName": "Google Nexus 5" }
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
capabilities = DesiredCapabilities.CHROME
capabilities = options.to_capabilities()
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities = capabilities)

  • 您可以在通过ChromeOptions()调用Remote():

    from selenium import webdriver
    mobile_emulation = { "deviceName": "Google Nexus 5" }
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
    driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', options=chrome_options)
    

  • 您在如果仍然看到错误,请执行以下升级/清理任务:

    If you are still seeing the error, perform the following upgradation/cleanup tasks:

    • 升级到当前级别 版本3.13.0 .
    • ChromeDriver 升级到当前的 ChromeDriver v2 .40 级别.
    • Chrome 版本保持在 Chrome v66-68 级别之间. (根据ChromeDriver v2.40发行说明)
    • 通过 IDE
    • 清理您的项目工作区重建您的项目,并且仅具有必需的依赖项.
    • (仅仅WindowsOS )使用 CCleaner 该工具可以消除执行 Test Suite 之前和之后的所有操作系统琐事.
    • (仅仅LinuxOS ) Revo Uninstaller ,并安装最新的GA和 Web Client 的发行版本.
    • 进行系统重启.
    • 始终在tearDown(){}方法中调用driver.quit()以关闭&优雅地销毁 WebDriver Web Client 实例.
    • 执行您的@Test.
    • Upgrade Selenium to current levels Version 3.13.0.
    • Upgrade ChromeDriver to current ChromeDriver v2.40 level.
    • Keep Chrome version between Chrome v66-68 levels. (as per ChromeDriver v2.40 release notes)
    • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
    • (WindowsOS only) Use CCleaner tool to wipe off all the OS chores before and after the execution of your Test Suite.
    • (LinuxOS only) Free Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint before and after the execution of your Test Suite.
    • If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
    • Take a System Reboot.
    • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
    • Execute your @Test.

    寻找针对特定错误的详细解决方案,我走进了 Amazon S3和通过对等方重置连接" ,其中Garry Dolley将问题原因归纳为以下提到的因素的组合:

    Looking out for a granular solution to specific error I stepped into Amazon S3 and "Connection Reset by Peer" where Garry Dolley summerizes the the cause of the problem to be a combination of the below mentioned factors:

    • TCP窗口缩放比例
    • Linux内核2.6.17或更高版本
    • TCP Window Scaling
    • Linux kernels 2.6.17 or newer

    Linux 2.6.17+内核增加了TCP窗口/缓冲区的最大大小,如果它不能处理足够大的TCP窗口,则会开始引起其他麻烦.齿轮将重置连接,我们将其视为对等连接重置"消息.

    Linux kernels 2.6.17+ increased the maximum size of the TCP window/buffer, and this started to cause other gear to wig out, if it couldn’t handle sufficiently large TCP windows. The gear would reset the connection, and we see this as a "Connection reset by peer" message.

    可能的解决方案是将以下条目放入/etc/sysctl.conf:

    A pottential solution will be to put the following entries within /etc/sysctl.conf:

    • net.ipv4.tcp_wmem = 4096 16384 512000
    • net.ipv4.tcp_rmem = 4096 87380 512000

    注意:此修补程序简单易用,但会减慢最大下载速度的最大交换速度.

    Note: This fix is easy peasy, but it’ll slow your maximum throughput inexchange of faster downloads.

    如果适用,请确保系统上的/etc/hosts包含以下条目:

    If applicable, ensure that /etc/hosts on your system contains the following entry :

    127.0.0.1               localhost.localdomain localhost
    


    相关讨论

    以下是一些相关讨论:


    Related Discussions

    Here are some of the related discussions:

    • Python socket.error: [Errno 104] Connection reset by peer
    • Yet Another 'Connection reset by peer' Error
    • Connection reset by peer [errno 104] in Python 2.7
    • Remote WebDriver UnreachableBrowserException: Could not start a new session
    • How to add selenium chrome options to 'desiredCapabilities'?

    以下是此讨论的参考:

    • Example Python Chrome Mobile Emulation Automated Unit Testing Using Selenium 2 WebDriver ChromeDriver
    • Amazon S3 and "Connection Reset by Peer"
    • How To: Network / TCP / UDP Tuning
    • How To : Free Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint
    • Mobile Emulation
    • selenium.webdriver.android.webdriver
    • Getting started with Selendroid