Python ConnectionAbortedError: [WinError 10053] 已建立的连接被主机中的软件中止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51562067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
提问by oldboy
For some reason I get the following error onlywhen I open up a nested webdriver
instance. No idea what is happening here.
出于某种原因,只有在打开嵌套webdriver
实例时才会出现以下错误。不知道这里发生了什么。
I am using Windows 10,geckodriver 0.21.0,and Python 3.7.
我使用的是Windows 10、geckodriver 0.21.0和Python 3.7。
ConnectionAbortedError: [WinError 10053]
ConnectionAbortedError: [WinError 10053]
An established connection was aborted by the software in your host machine
Part of Script That Is Working Fine
运行良好的脚本部分
tab_backers = ff.find_element_by_xpath('//a[@gogo-test="backers_tab"]')
try:
funding_backers_count = int(''.join(filter(str.isdigit, str(tab_backers.text))))
except ValueError:
funding_backers_count = 0
if funding_backers_count > 0:
tab_backers.click()
see_more_backers = WebDriverWait(ff, 10).until(
EC.element_to_be_clickable((By.XPATH, '//ui-view//a[text()="See More Backers"]'))
)
clicks = 0
while clicks < 0:
clicks += 1
ff.WebDriverWait(ff, 5).until(
see_more_backers.click()
)
for container in ff.find_elements_by_xpath('//ui-view//div[@class="campaignBackers-pledge ng-scope"]'):
backers_profile = container.find_elements_by_xpath('./*/div[@class="campaignBackers-pledge-backer-details"]/a')
if len(backers_profile) > 0:
backers_profile = backers_profile[0].get_attribute('href')
else:
backers_profile = 'Unknown'
backers_name = safe_encode(container.find_element_by_xpath('(./*/div[@class="campaignBackers-pledge-backer-details"]/*)[1]').text)
backers_timestamp = container.find_element_by_xpath('./*/div[@class="campaignBackers-pledge-backer-details"]/div[contains(@class, "campaignBackers-pledge-backer-details-note")]').text
backers_contribution = container.find_element_by_xpath('./*//*[contains(@class, "campaignBackers-pledge-amount-bold")]').text
if backers_contribution != 'Private':
backers_contribution = int(''.join(filter(str.isdigit, str(backers_contribution))))
if backers_profile != 'Unknown':
Part of Script Causing System to Abort Connection
部分脚本导致系统中止连接
_ff = create_webdriver_instance()
_ff.get(backers_profile)
_ff.quit()
Traceback
追溯
Traceback (most recent call last):
File "C:\Users\Anthony\Desktop\test.py", line 271, in <module>
backers_profile = container.find_elements_by_xpath('./*/div[@class="campaignBackers-pledge-backer-details"]/a')
File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 381, in find_elements_by_xpath
return self.find_elements(by=By.XPATH, value=xpath)
File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 680, in find_elements
{"using": by, "value": value})['value']
File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 318, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 472, in execute
return self._request(command_info[0], url, body=data)
File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 495, in _request
self._conn.request(method, parsed_url.path, body, headers)
File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1055, in _send_output
self.send(chunk)
File "C:\Users\Anthony\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 977, in send
self.sock.sendall(data)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
geckodriver.log
壁虎驱动程序日志
Here it is in a codepen,since it is wayyy too long!
这是在代码笔中,因为它太长了!
create_webdriver_instance Function
create_webdriver_instance 函数
def create_webdriver_instance():
options = Options()
options.add_argument('-headless')
try:
ua_string = random.choice(ua_strings)
profile = webdriver.FirefoxProfile()
profile.set_preference('general.useragent.override', ua_string)
return webdriver.Firefox(profile) # profile, firefox_options=options
except IndexError as error:
print('\nSection: Function to Create Instances of WebDriver\nCulprit: random.choice(ua_strings)\nIndexError: {}\n'.format(error))
return webdriver.Firefox() # firefox_options=options
Does anybody have any idea whatsoever what might be causing the connection to abort?
有没有人知道什么可能导致连接中止?
回答by DebanjanB
This error message...
这个错误信息...
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
...implies that the initialization of a new WebBrowsing Sessioni.e. Firefox Browsersession was aborted.
...暗示新的WebBrowsing 会话(即Firefox 浏览器会话)的初始化已中止。
An established connection was aborted by the software in your host machine
已建立的连接被主机中的软件中止
As per your code attempt the error is clearly coming out create_webdriver_instance()
function which contains:
根据您的代码尝试,错误显然是出来的create_webdriver_instance()
功能,其中包含:
try:
ua_string = random.choice(ua_strings)
profile = webdriver.FirefoxProfile()
profile.set_preference('general.useragent.override', ua_string)
return webdriver.Firefox(profile)
And:
和:
except IndexError as error:
print('\nSection: Function to Create Instances of WebDriver\nCulprit: random.choice(ua_strings)\nIndexError: {}\n'.format(error))
return webdriver.Firefox()
So it is not exactly clear from which function you are facing this issue among return webdriver.Firefox(profile)
or webdriver.Firefox()
.
因此,目前尚不清楚您在return webdriver.Firefox(profile)
或 中遇到此问题的哪个函数webdriver.Firefox()
。
Perhaps a closer look at the logs within codepensuggests the error is coming out from webdriver.Firefox(profile)
.
也许仔细查看codepen中的日志表明错误来自webdriver.Firefox(profile)
.
Reasons
原因
There can be multiple reason behind this error:
此错误背后可能有多种原因:
- Presence of anti-virus softwares.
- Firewall blocking the ports.
- Network Configuration.
- Problem can be caused by CORS.
- Due to enabling of HTTP keep-alive connections
- 防病毒软件的存在。
- 防火墙阻塞端口。
- 网络配置。
- 问题可能是由CORS引起的。
- 由于启用了 HTTP 保持活动连接
Solution
解决方案
The initial step would be find out if any software is blocking the connection to/from the server in your computer. Apart from that the probable solutions are:
第一步是找出是否有任何软件阻止了与您计算机中服务器的连接。除此之外,可能的解决方案是:
- Disable anti-virus softwares.
- Disable firewall.
Ensure that /etc/hosts on your system contains the following entry:
127.0.0.1 localhost.localdomain localhost
As per Connection was aborted by the software in your host machineyou need to allow localhost routes like
http://localhost:8080/reactive-commands
AutomatedTester: This issue is not because we are not connected at the time of doing a request. If that was the issue we would be getting a httplib.HTTPConnection exception being thrown. Instead a BadStatusLine is thrown when we do a connection and close it and try parse the response. Now this could be the python stdlib bug, httplib bug or selenium bug. A Python client to replace urllib with something else that does not exhibit the same defect with Keep-Alive connections is WIP.
andreastt: The geckodriver team is working on extending the server-side timeout value to something more reasonable. As I said, this would help mitigate this issue but not fundamentally fix it. In any case it is true that five seconds is probably too low to get real benefit from persistent HTTP connections, and that increasing it to something like 60 seconds would have greater performance.
- 禁用杀毒软件。
- 禁用防火墙。
确保系统上的 /etc/hosts 包含以下条目:
127.0.0.1 localhost.localdomain localhost
根据连接被主机中的软件中止,您需要允许 localhost 路由,例如
http://localhost:8080/reactive-commands
根据Keep-Alive 与 geckodriver 0.21.0 的连接,在不活动 5 秒后使用 Selenium Python 客户端重新连接时断开
AutomatedTester:这个问题不是因为我们在执行请求时没有连接。如果这是问题,我们将抛出 httplib.HTTPConnection 异常。相反,当我们建立连接并关闭它并尝试解析响应时,会抛出 BadStatusLine。现在这可能是 python stdlib 错误、httplib 错误或 selenium 错误。将 urllib 替换为与 Keep-Alive 连接没有相同缺陷的其他内容的 Python 客户端是WIP。
andreastt:该geckodriver团队正在扩展服务器端超时值会更合理。正如我所说,这将有助于缓解这个问题,但不能从根本上解决它。在任何情况下,5 秒可能太短而无法从持久的 HTTP 连接中获得真正的好处,而将其增加到 60 秒之类的时间会获得更好的性能。
Conclusion
结论
Selenium 3.14.0
has just been released. If you are affected by this issue please upgrade accordingly.
Selenium 3.14.0
刚刚被释放。如果您受到此问题的影响,请相应升级。
References:
参考:
- Flask broken pipe with requests
- Cross-Origin Resource Sharing (CORS)
- Connection was aborted by the software in your host machine
- [WinError 10053] with the 0.21.0
- Keep-Alive connection to geckodriver 0.21.0 dropped after 5s of inactivity without re-connection using Selenium Python client
- Support keep alive connections
- Struct hyper::server::Server
- Urllib3
回答by Andrei Suvorkov
As documentationsays:
正如文档所说:
Software caused connection abort. An established connection was aborted by the software in your host computer, possibly due to a data transmission time-out or protocol error.
软件导致连接中止。已建立的连接被主机中的软件中止,可能是由于数据传输超时或协议错误。
Possible reasons:
可能的原因:
- Timeout or other network-level error.
- Network connection died
- Firewall closed the connection because it was open too long
- Connection was closed before process has been finished
- AntiVirus blocks the connection
- 超时或其他网络级错误。
- 网络连接中断
- 防火墙关闭了连接,因为它打开的时间太长
- 在进程完成之前连接已关闭
- 防病毒软件阻止连接
and etc.
等等。
Also try to downgrade geckodriver 0.21.0
to geckodriver 0.20.1
. You can download it here. It seems to be a problem with geckodriver 0.21.0
https://stackoverflow.com/a/51236719/8625512
也尝试降级geckodriver 0.21.0
到geckodriver 0.20.1
. 你可以在这里下载。https://stackoverflow.com/a/51236719/8625512好像有问题geckodriver 0.21.0
PS:
PS:
options.add_argument('-headless')
should be:
应该:
options.add_argument('--headless')
回答by Carlos Ribeiro
This problem happened to me and as the error was intermittent, I initially believed that it was some firewall or antivirus problem, but it was much simpler.
这个问题发生在我身上,由于错误是间歇性的,我最初认为这是一些防火墙或防病毒问题,但它要简单得多。
I had a form that was being submitted twice when the SEND button was clicked. The button was set to type="submit" and a javascript code submitted this form when this button was clicked. I changed the button to type="button" and the problem was solved.
单击“发送”按钮时,我有一个表单被提交了两次。该按钮被设置为 type="submit" 并且当单击该按钮时一个 javascript 代码提交了这个表单。我将按钮更改为 type="button",问题就解决了。
回答by user899575
Windows has implemented Controlled folder access, which is to block un-authorized app to access your important files(means almost anything installed by windows). This is to prevent data encryption and ransom by malware.
Windows 实现了受控文件夹访问,即阻止未经授权的应用程序访问您的重要文件(意味着几乎所有由 Windows 安装的文件)。这是为了防止恶意软件对数据进行加密和勒索。
Windows will pop-out notification if it blocks any app.
如果 Windows 阻止任何应用程序,它会弹出通知。
You can allow apps/programs access through Controlled folder access.
您可以通过受控文件夹访问权限允许应用程序/程序访问。
回答by TheGuardener
Another reason:When a page served by python is loading ,user may cancelthe loading before tranmission is complete,by pressing Escor by clicking 'X'(Stop loading this page button).This is usually done when the server is slow. Eg if user goes to 1and Press Esc before the image is fully loaded,this error will occur.In that case,you have to Handle it.
另一个原因:当python服务的页面正在加载时,用户可以在传输完成之前取消加载,按Esc或单击'X'(停止加载此页面按钮)。这通常在服务器缓慢时执行。例如,如果用户在图像完全加载之前转到1并按 Esc,则会发生此错误。在这种情况下,您必须处理它。