Python connectionreseterror 104 connection reset by peer

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection reset by peer when sending POST #4937

Connection reset by peer when sending POST #4937

Comments

Client code that uses Requests module to send data via HTTP POST encounters a ConnectionResetError . The entire operation (composed of multiple POST requests to a small set of service endpoints) can sometimes succeed, but most of the time, it fails with this error.

Expected Result

Operation succeeds (or fails) without connection issues.

Actual Result

The operation fails with ConnectionResetError .

Additional Information

It’s a little difficult to provide basic reproduction for the issue as we’re running into this problem with a test payload that’s specific to our system. The server (peer) is a Java application that’s configured to terminate/reset connection after a given time of no use (idle). The client sends multiple one-off POST requests, but it seems like internally, the connections are being reused similar to issue #4506, and the operation eventually runs into a connection that’s already been reset and raises the error. However, unlike #4506, we are not using Sessions.

Читайте также:  После загрузки формы html

Here are some tracebacks that could hopefully describe the problem better:

Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen chunked=chunked) File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 384, in _make_request six.raise_from(e, None) File "", line 2, in raise_from File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 380, in _make_request httplib_response = conn.getresponse() File "/usr/local/lib/python3.7/http/client.py", line 1321, in getresponse response.begin() File "/usr/local/lib/python3.7/http/client.py", line 296, in begin version, status, reason = self._read_status() File "/usr/local/lib/python3.7/http/client.py", line 257, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/usr/local/lib/python3.7/socket.py", line 589, in readinto return self._sock.recv_into(b) ConnectionResetError: [Errno 104] Connection reset by peer During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send timeout=timeout File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 638, in urlopen _stacktrace=sys.exc_info()[2]) File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 367, in increment raise six.reraise(type(error), error, _stacktrace) File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 685, in reraise raise value.with_traceback(tb) File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen chunked=chunked) File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 384, in _make_request six.raise_from(e, None) File "", line 2, in raise_from File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 380, in _make_request httplib_response = conn.getresponse() File "/usr/local/lib/python3.7/http/client.py", line 1321, in getresponse response.begin() File "/usr/local/lib/python3.7/http/client.py", line 296, in begin version, status, reason = self._read_status() File "/usr/local/lib/python3.7/http/client.py", line 257, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/usr/local/lib/python3.7/socket.py", line 589, in readinto return self._sock.recv_into(b) urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer')) . Traceback (most recent call last): . File "../client-code.py", line 322, in createFile r = requests.post(fileSubmissionsUrl, data=json.dumps(fileToCreateObject), headers=self.headers) File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 116, in post return request('post', url, data=data, json=json, **kwargs) File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 60, in request return session.request(method=method, url=url, **kwargs) File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 533, in request resp = self.send(prep, **send_kwargs) File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 646, in send r = adapter.send(request, **kwargs) File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 498, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer')) 

System Information

< "chardet": < "version": "3.0.4" >, "cryptography": < "version": "2.3.1" >, "idna": < "version": "2.6" >, "implementation": < "name": "CPython", "version": "3.7.1" >, "platform": < "release": "4.14.42-61.37.amzn2.x86_64", "system": "Linux" >, "pyOpenSSL": < "openssl_version": "1000211f", "version": "18.0.0" >, "requests": < "version": "2.20.1" >, "system_ssl": < "version": "20000000" >, "urllib3": < "version": "1.24.1" >, "using_pyopenssl": true > 

The text was updated successfully, but these errors were encountered:

Читайте также:  Css font size all page

Источник

Python connectionreseterror 104 connection reset by peer

Last updated: Jun 20, 2023
Reading time · 3 min

banner

# Python socket.error: [Errno 104] Connection reset by peer

The Python error «socket.error: [Errno 104] Connection reset by peer» occurs when the remote server immediately drops the connection rather than initiating the usual handshake.

Use a try/except statement to handle the error.

Copied!
from socket import error as SocketError import errno import urllib.request url = 'http://www.python.org/' try: with urllib.request.urlopen(url, timeout=10) as f: print(f.read()) except SocketError as err: if err.errno != errno.ECONNRESET: # The error is NOT a ConnectionResetError raise # 👇️ Handle the ConnectionResetError here print('ConnectionResetError occurred')

handle connection reset error using try except

The code sample uses a try/except statement to handle the potential ConnectionResetError .

We first make an HTTP request in the try block.

If an error occurs, the except block runs.

We check if the error is not equal to errno.ECONNRESET .

If the remote server hasn’t reset the connection, then the error is NOT ConnectionResetError , so we re-raise it.

You can handle the ConnectionResetError exception right after the if statement.

For example, you could re-issue the request.

You can also explicitly handle the ConnectionResetError in the except block.

Copied!
import urllib.request url = 'http://www.python.org/' try: with urllib.request.urlopen(url, timeout=10) as f: print(f.read()) raise ValueError('hi') except ConnectionResetError: print('A ConnectionResetError occurred') except TimeoutError: print('A TimeoutError occurred') except BaseException as e: print('A generic error occurred', e)

If a ConnectionResetError occurs, the first except block runs.

The second except block handles a TimeoutError exception.

The last except block handles generic errors that are neither ConnectionResetError nor TimeoutError .

# Try to wait for N seconds between the requests

Another thing you can try is to wait for N seconds between the requests as the remote server might have throttled you.

Copied!
import errno from socket import error as SocketError import urllib.request import time url = 'http://www.python.org/' try: # ✅ Wait for 2 seconds. time.sleep(2) with urllib.request.urlopen(url, timeout=10) as f: print(f.read()) except SocketError as err: if err.errno != errno.ECONNRESET: # The error is NOT a ConnectionResetError raise # 👇️ Handle the ConnectionResetError here print('ConnectionResetError occurred')

The code sample uses the time.sleep method to wait for 2 seconds before issuing the HTTP request.

Читайте также:  How to throw exceptions in java

The remote server you are making requests to might have limited the number of connections.

This is often a cause of the error because it occurs when the remote server immediately drops the connection rather than initiating the usual handshake.

You can test different values for the time.sleep() method.

This approach is especially useful when you have to issue multiple requests (e.g. in a for loop).

You can also try to check how many requests you are allowed to issue per (second, hour or day) in the API’s documentation.

# Try setting the headers when making the request

The server might’ve also dropped the connection because it detected that the client is a Python script.

You can set the User-Agent header to try to get around this.

Let’s look at an example that uses the requests library.

First, make sure you have the requests module installed.

Copied!
pip install requests # or with pip3 pip3 install requests

Now import and use the module as follows.

Copied!
import errno from socket import error as SocketError import requests url = 'http://www.python.org/' headers = "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)" > try: response = requests.get(url, headers=headers, timeout=10) print(response.text) print(response.status_code) except SocketError as err: if err.errno != errno.ECONNRESET: # The error is NOT a ConnectionResetError raise # 👇️ Handle the ConnectionResetError here print('ConnectionResetError occurred')

If you expect a JSON response, you would use the response.json() method instead of accessing the response.text attribute.

Copied!
parsed = response.json() print(parsed)

The headers dictionary sets the User-Agent header to simulate an HTTP request that is made by a browser and not a Python script.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Оцените статью