Error 104 connection reset by peer python

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

[Errno 104] Connection reset by peer #16

[Errno 104] Connection reset by peer #16

Comments

I am getting quite often the following error when uploading files:
urllib2.URLError:

What can I do to troubleshoot the issue ?

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

Here is the full error stack.

It consistently happens after ~2 min on different connections (home and work) and different computers.

I usually manage to upload one file a day. And after I get those errors.
The files I am uploading are about 5 GB size.
Is there a daily upload cap ?

Getting upload URL. Traceback (most recent call last): File "/home/someuser/bin/b2", line 949, in main() File "/home/someuser/bin/b2", line 942, in main upload_file(args) File "/home/someuser/bin/b2", line 702, in upload_file response = post_file(url, headers, local_file) File "/home/someuser/bin/b2", line 313, in post_file with OpenUrl(url, data_file, headers) as response_file: File "/home/someuser/bin/b2", line 276, in __enter__ self.file = urllib2.urlopen(request) File "/usr/lib64/python2.7/urllib2.py", line 154, in urlopen return opener.open(url, data, timeout) File "/usr/lib64/python2.7/urllib2.py", line 431, in open response = self._open(req, data) File "/usr/lib64/python2.7/urllib2.py", line 449, in _open '_open', req) File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain result = func(*args) File "/usr/lib64/python2.7/urllib2.py", line 1240, in https_open context=self._context) File "/usr/lib64/python2.7/urllib2.py", line 1197, in do_open raise URLError(err) urllib2.URLError: Getting upload URL. Traceback (most recent call last): File "/home/someuser/bin/b2", line 949, in main() File "/home/someuser/bin/b2", line 942, in main upload_file(args) File "/home/someuser/bin/b2", line 702, in upload_file response = post_file(url, headers, local_file) File "/home/someuser/bin/b2", line 313, in post_file with OpenUrl(url, data_file, headers) as response_file: File "/home/someuser/bin/b2", line 276, in __enter__ self.file = urllib2.urlopen(request) File "/usr/lib64/python2.7/urllib2.py", line 154, in urlopen return opener.open(url, data, timeout) File "/usr/lib64/python2.7/urllib2.py", line 431, in open response = self._open(req, data) File "/usr/lib64/python2.7/urllib2.py", line 449, in _open '_open', req) File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain result = func(*args) File "/usr/lib64/python2.7/urllib2.py", line 1240, in https_open context=self._context) File "/usr/lib64/python2.7/urllib2.py", line 1197, in do_open raise URLError(err) urllib2.URLError:

That’s puzzling. I just tested myself with a 5GB upload and it worked fine.

Читайте также:  Изучаем php mysql моррисон

The spending cap feature on B2 accounts is now working, so if you’re over your storage limit, it will deny uploads. You can check your status on the B2 accounts page.

Another thing to check is that your files are not bigger than 5,000,000,000 bytes. I should have done so before, but I just added an explicit check to the b2 tool to verify the file size before trying to upload.

If the spending cap is hit, user should get the «storage_cap_exceeded» error instead of a closed socket.

Источник

Python Handling Socket.Error: [Errno 104] Connection Reset by Peer

Python Handling Socket.Error: [Errno 104] Connection Reset by Peer

  1. What is the Connection Reset by Peer Error in Python
  2. Likely Causes of Connection Reset by Peer Error in Python
  3. Solutions to the Connection Reset by Peer Error in Python
  4. Conclusion

After we have created an application that connects to a server to download information, like forex or bitcoin rates, we tend to encounter all sorts of connection issues; one such issue is the [Errno 104] Connection reset by peer error.

What is the Connection Reset by Peer Error in Python

This error usually occurs when a client tries to connect to a server, the server receives this connection, but the connection is closed before the client receives the information it expects to receive from the server.

It is likened to a situation where you make a call on a telephone, the receiver picks up the call and hangs up almost immediately, or the connection gets lost immediately after the receiver picks up. When such happens, the connection is reset.

When a connection is reset by peer , it doesn’t necessarily mean the connection was closed by the server; the connection can be closed by the client too. For instance, a caller could hang up the call immediately after the receiver picks up.

Then the connection can also be closed by the devices serving to connect the client to the server, for instance, a router.

Likely Causes of Connection Reset by Peer Error in Python

While it’s difficult to pinpoint the main causes of this error due to its unpredictable nature, we have listed the most likely causes.

  1. Server crash: When we try to get a response from a server that has crashed and trying to reboot, it will most likely throw the errno 104 right back in your face.
  2. Bugs and Connectivity Issues: Unstable network connectivity and bugs in the Python application we are using to get a response from the server could cause the connection between the client and server to terminate unexpectedly and throw the connection reset error.
  3. Server Bandwidth Reaching Threshold: In a situation where we are trying to get a response from a server that has reached its limited number of connections, the server will reject a new connection since there is no more room and throw the errno 104 .
Читайте также:  Python красиво вывести словарь

Solutions to the Connection Reset by Peer Error in Python

The errno 104 is volatile and extremely difficult to reproduce; hence, solutions to counter such errors are few and far between. But there are some troubleshooting that we can practice.

For one, we can check our connections to ensure they are stable and at high speed; epileptic network connectivity is one problem that cuts across various errors.

Also, we can check that our Python application does not have bugs; we can do this by checking if the address we are trying to connect to is spelled well, or we can try another server address to see what happens.

We can also check the server we are trying to get a response from is fully functional and can service requests.

Conclusion

Encountering errors and problems while coding can be annoying, but the best approach is to calm down and slowly assess the problem. The solution can be as simple as retrying the connection to the server.

Fisayo is a tech expert and enthusiast who loves to solve problems, seek new challenges and aim to spread the knowledge of what she has learned across the globe.

Related Article — Python Error

Источник

Error 104 connection reset by peer python

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.

Читайте также:  Function to call php script

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.

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.

Источник

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