简体   繁体   中英

Python socket ConnectionResetError: [Errno 54] Connection reset by peer vs socket.error: [Errno 104] Connection reset by peer

I'm having trouble debugging my code because I cannot understand the socket error being raised. Here is the traceback.

Traceback (most recent call last):
 File "clickpression.py", line 517, in <module> presser.main()
 File "clickpression.py", line 391, in main
 File "clickpression.py", line 121, in clickpress self.refresh_proxies(country=country)
 File "clickpression.py", line 458, in refresh_proxies self.proxies = self.get_proxies(country=country)
 File "helpers.py", line 72, in wrapper return func(*args, **kwargs)
 File "clickpression.py", line 264, in get_proxies self.settings.SUPER_PROXY).read().decode('utf-8')
 File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 161, in urlopen return opener.open(url, data, timeout)
 File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 463, in open response = self._open(req, data)
 File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 481, in _open '_open', req)
 File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 441, in _call_chain result = func(*args)
 File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 1210, in http_open return self.do_open(http.client.HTTPConnection, req)
 File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 1185, in do_open r = h.getresponse()
 File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1171, in getresponse response.begin()
 File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 351, in begin version, status, reason = self._read_status()
 File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 313, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
 File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", line 374, in readinto return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer

According to the errno library Errno 54 is errno.EXFULL which in the python 3 documentation is explained as exchange full .

To my understanding the Connection reset by peer is Errno 104 ie errno.ECONNRESET .

So what does errno.EXFULL mean? and why does socket raise the error with a connection reset by peer description instead of exchange full . And or how are the two errors errno.EXFULL and errno.ECONNRESET related?

PS: I read that the errno 54 might be related to http proxy (I'm using a proxy in my code). If so, how?

According to the errno library Errno 54 is errno.EXFULL

Did you determine that by examining errno.errorcode[54] ? Anyway - this errno library might be at fault. You could verify the meaning of an error code on your system by looking into errno.h , eg with the help of gcc :

gcc -xc -imacros errno.h -Wp,-P -E <(echo ECONNRESET)

Also, the Python documentation says:

To translate a numeric error code to an error message, use os.strerror().

It may well be that error number 54 is ECONNRESET on your system, and that os.strerror(54) will attest that.

Now that you have verified that os.strerror(54) returns 'Exchange full', I am puzzled why the error number 54 and the error string Connection reset by peer do not match. If that happens on a system with strace or something similar, I would further check which error is returned by the operating system through use of strace -e network on the affected process.

Regarding your question about EXFULL: Its meaning seems somewhat system dependent; eg on Linux, EXFULL is returned from only a handful places in the kernel, the only network-related place being in br_if.c concerning network bridges, when no available bridge port number is found (other places are in USB and SCSI drivers).

I tried to use python to crew coin market on OKEX.com using WebSocket,cause the url is an outer address,i used a vpn service provided by us,but it still can work. here is the code an traceback.

from ws4py.client.threadedclient import WebSocketClient


class DummyClient(WebSocketClient):
    def opened(self):
      # self.send("{'event': 'addChannel', 'channel': 'ok_sub_futureusd_btc_ticker_this_week'}") #发送请求数据格式
         # self.send("www.baidu.com")
         self.send("{'event':'addChannel','channel':'ok_sub_spot_bch_btc_ticker'}")
    def closed(self, code, reason=None):
        print("Closed down", code, reason)

#服务器返回消息
    def received_message(self, m):
        print("recv:", m)


if __name__ == '__main__':

    try:
        # 服务器连接地址wss://real.okex.com:10440/websocket/okexapi
       # ws = DummyClient('wss://real.okcoin.cn:10440/websocket/okcoinapi', protocols=['chat'])
        ws = DummyClient('wss://real.okex.com:10440/websocket/okexapi', protocols=['chat'])
        ws.connect()
        #ws.send("my test...")
        ws.run_forever()
    except KeyboardInterrupt:
        ws.close()

在此处输入图像描述

You can try this code to your project:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

if it not work,make sure the server open TLSv1 support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM