为什么 Python 请求总是给我这个错误? “现有连接被远程主机强制关闭”

发布于 2025-01-11 05:40:45 字数 5961 浏览 0 评论 0原文

我试图每十秒向 Binance api(加密货币交易所)发送一个请求,以获取特定加密货币符号的最新价格。然而,5 到 20 小时后,它停止工作并抛出此错误:

回溯(最近一次调用最后一次):文件 “C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py”, 第 703 行,在 urlopen 中 httplib_response = self._make_request( 文件 "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", _make_request 中的第 386 行 self._validate_conn(conn) 文件“C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py”, 第 1040 行,位于 validate_conn 中 conn.connect() 文件“C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py”, 416号线,连接中 self.sock = ssl_wrap_socket( 文件 "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\ssl.py", 第 449 行,在 ssl_wrap_socket 中 ssl_sock = ssl_wrap_socket_impl( File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\ssl.py", 第 493 行,在 _ssl_wrap_socket_impl 中 返回 ssl_context.wrap_socket(sock, server_hostname=server_hostname) 文件 “C:\ Users \ nrspa \ AppData \ Local \ Programs \ Python \ Python310 \ lib \ ssl.py”, 第512行,在wrap_socket中 return self.sslsocket_class._create( 文件 "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\ssl.py", 第 1070 行,在 _create 中 self.do_handshake() 文件“C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\ssl.py”, 第 1341 行,在 do_handshake 中 self._sslobj.do_handshake() ConnectionResetError: [WinError 10054] 现有连接被远程主机强制关闭

在处理上述异常的过程中,又发生了一个异常:

回溯(最近一次调用最后一次):文件 “C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\adapters.py”, 第 440 行,发送中 resp = conn.urlopen( 文件 "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", 第 785 行,在 urlopen 中 retries = retries.increment( 文件 "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\retry.py", 第 550 行,增量 raise Six.reraise(type(error), error, _stacktrace) 文件 "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\packages\six.py", 第 769 行,加注中 提高 value.with_traceback(tb) 文件“C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py”, 第 703 行,在 urlopen 中 httplib_response = self._make_request( 文件 "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", _make_request 中的第 386 行 self._validate_conn(conn) 文件“C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py”, 第 1040 行,位于 validate_conn 中 conn.connect() 文件“C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py”, 416号线,连接中 self.sock = ssl_wrap_socket( 文件 "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\ssl.py", 第 449 行,在 ssl_wrap_socket 中 ssl_sock = ssl_wrap_socket_impl( File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\ssl.py", 第 493 行,在 _ssl_wrap_socket_impl 中 返回 ssl_context.wrap_socket(sock, server_hostname=server_hostname) 文件 “C:\ Users \ nrspa \ AppData \ Local \ Programs \ Python \ Python310 \ lib \ ssl.py”, 第512行,在wrap_socket中 return self.sslsocket_class._create( 文件 "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\ssl.py", 第 1070 行,在 _create 中 self.do_handshake() 文件“C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\ssl.py”, 第 1341 行,在 do_handshake 中 self._sslobj.do_handshake() urllib3.exceptions.ProtocolError: ('连接中止。', ConnectionResetError(10054, '现有的 连接被远程主机强制关闭', 无, 10054, 无))

在处理上述异常的过程中,又发生了一个异常:

回溯(最近一次调用最后一次):文件 “c:\Users\nrspa\Desktop\CryptoBot v2.6 (24h)\main (market).py”,行 113、在 CB.main() 文件“c:\Users\nrspa\Desktop\CryptoBot v2.6 (24h)\main (market).py”,第 68 行,在 main 中 self.array = self.create_array() 文件“c:\Users\nrspa\Desktop\CryptoBot v2.6 (24h)\main (market).py”,行 46、在create_array中 self.array.append(self.get_price()) 文件“c:\Users\nrspa\Desktop\CryptoBot v2.6 (24h)\main (market).py”,行 22、在get_price中 response = requests.get(url, headers = {"User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML,如 Gecko) Chrome/51.0.2704.103 Safari/537.36"}) 文件 “C:\ Users \ nrspa \ AppData \ Local \ Programs \ Python \ Python310 \ lib \ site-packages \ requests \ api.py”, 第 75 行,在 get 中 返回请求('get',url,params=params,**kwargs)文件“C:\ Users \ nrspa \ AppData \ Local \ Programs \ Python \ Python310 \ lib \ site-packages \ requests \ api.py”, 第 61 行,根据要求 返回 session.request(method=method, url=url, **kwargs) 文件 "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", 第 529 行,根据要求 resp = self.send(prep, **send_kwargs) 文件 "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", 第 645 行,发送中 r = adapter.send(request, **kwargs) 文件“C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\adapters.py”, 第 501 行,发送中 raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('连接中止。', ConnectionResetError(10054, '现有连接被强制 被远程主机关闭', None, 10054, None))

另请注意,这是我的代码(它是一个类的一部分),它每 10 秒从 Binance 获取一次价格,直到达到所需的数组长度,此时它处理数据:

import json
import requests
import time

def get_price(self):
    url = f"https://api.binance.com/api/v3/ticker/price?symbol={self.symbol}" 
    response = requests.get(url, headers = {"User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"})
    json_response = json.loads(response.text)
    return float(json_response['price'])

def create_array(self):
    i = len(self.array)
    if i < self.array_len:
        while i < self.array_len:
            time.sleep(self.time_interval)
            self.array.append(self.get_price())
            i = i + 1
        return self.array
    else:
        main()

I am attempting to send a request to Binance api (crypto exchange) for the latest price of a specific symbol of crypto every ten seconds. However after between 5 to 20 hours it stops working throwing this error:

Traceback (most recent call last): File
"C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py",
line 703, in urlopen
httplib_response = self._make_request( File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py",
line 386, in _make_request
self._validate_conn(conn) File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py",
line 1040, in validate_conn
conn.connect() File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py",
line 416, in connect
self.sock = ssl_wrap_socket( File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\ssl
.py",
line 449, in ssl_wrap_socket
ssl_sock = ssl_wrap_socket_impl( File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\ssl.py",
line 493, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname) File
"C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\ssl.py",
line 512, in wrap_socket
return self.sslsocket_class._create( File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\ssl.py",
line 1070, in _create
self.do_handshake() File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\ssl.py",
line 1341, in do_handshake
self._sslobj.do_handshake() ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File
"C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\adapters.py",
line 440, in send
resp = conn.urlopen( File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py",
line 785, in urlopen
retries = retries.increment( File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\retry.py",
line 550, in increment
raise six.reraise(type(error), error, _stacktrace) File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\packages\six.py",
line 769, in reraise
raise value.with_traceback(tb) File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py",
line 703, in urlopen
httplib_response = self._make_request( File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py",
line 386, in _make_request
self._validate_conn(conn) File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py",
line 1040, in validate_conn
conn.connect() File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py",
line 416, in connect
self.sock = ssl_wrap_socket( File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\ssl
.py",
line 449, in ssl_wrap_socket
ssl_sock = ssl_wrap_socket_impl( File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\ssl.py",
line 493, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname) File
"C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\ssl.py",
line 512, in wrap_socket
return self.sslsocket_class._create( File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\ssl.py",
line 1070, in _create
self.do_handshake() File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\ssl.py",
line 1341, in do_handshake
self._sslobj.do_handshake() urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, 'An existing
connection was forcibly closed by the remote host', None, 10054,
None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File
"c:\Users\nrspa\Desktop\CryptoBot v2.6 (24h)\main (market).py", line
113, in
CB.main() File "c:\Users\nrspa\Desktop\CryptoBot v2.6 (24h)\main (market).py", line 68, in main
self.array = self.create_array() File "c:\Users\nrspa\Desktop\CryptoBot v2.6 (24h)\main (market).py", line
46, in create_array
self.array.append(self.get_price()) File "c:\Users\nrspa\Desktop\CryptoBot v2.6 (24h)\main (market).py", line
22, in get_price
response = requests.get(url, headers = {"User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/51.0.2704.103 Safari/537.36"}) File
"C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py",
line 75, in get
return request('get', url, params=params, **kwargs) File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py",
line 61, in request
return session.request(method=method, url=url, **kwargs) File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py",
line 529, in request
resp = self.send(prep, **send_kwargs) File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py",
line 645, in send
r = adapter.send(request, **kwargs) File "C:\Users\nrspa\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\adapters.py",
line 501, in send
raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.',
ConnectionResetError(10054, 'An existing connection was forcibly
closed by the remote host', None, 10054, None))

Also note that this is my code (it is part of a class) it gets the price from Binance every 10 seconds until the desired array length is reached, at which point it processes the data:

import json
import requests
import time

def get_price(self):
    url = f"https://api.binance.com/api/v3/ticker/price?symbol={self.symbol}" 
    response = requests.get(url, headers = {"User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"})
    json_response = json.loads(response.text)
    return float(json_response['price'])

def create_array(self):
    i = len(self.array)
    if i < self.array_len:
        while i < self.array_len:
            time.sleep(self.time_interval)
            self.array.append(self.get_price())
            i = i + 1
        return self.array
    else:
        main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木落 2025-01-18 05:40:45

这个问题可能是由双方之间的“分歧”引起的。可能是连接超时或服务器关闭了它。

修复方法是捕获此异常并再次打开连接。

The issue can be caused by a "disagreement" between both sides. It can be that the connection timed out or the server closed it.

The fix is to catch this exception and open the connection again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文