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

Connection reset by peer [errno 104] in Python 2.7

i) Your try-except block doesn't catch any exceptions.

The first argument to except must be the type of the exception you want to catch. socket.errno is not an exception class but a module. You need to catch socket.error:

 except socket.error, ex:
print ex

It "crashes" your code because any exception that isn't handled somewhere in the call stack propagates outwards until it hits an except. If there is no handler the program is terminated.

ii) When the client terminates without closing the connection, a RST packet is sent by the TCP/IP stack of your OS. This is roughly the equivalent of hanging up a phone without saying goodbye. Python converts this into an exception with the text "Connection reset by peer". It simply means that since you called read() Python assumed you expect to receive something and when the connection suddenly disconnected, Python informs you of this by raising the exception.

Python requests / urllib2 socket.error: [Errno 104] Connection reset by peer

Your code is correct. The error might mean that the server on the other end is unhappy about what you're sending. You have to make sure you send it an appropriate request. To do that, you can:

  • Read the documentation about the host
  • Contact its owner
  • Check what your browser is sending when you successfully access your data

For the third option, use the integrated development tools on firefox, chrome, safari or your favorite browser (e.g for Firefox, read this)

ConnectionResetError: [Errno 104] Connection reset by peer python 3

I would suggest you to use requests library in python.For which you can read over here http://docs.python-requests.org/en/master/.
Secondly I tried to make a get request to the URL you had mentioned and i got a 401 - UnAuthorized as the response. The reason being that I do not have the headers.
If you want you can use the following piece of code i modified for you.

import requests
def getCandles( currency="EUR_USD", count=500, granularity="D"):

headers = getHeader()
base_url = 'https://api-fxtrade.oanda.com/'
ins_url = 'v3/instruments/{}/candles/?count={}&price=M&granularity={}'.format( currency, count, granularity)
url = base_url + ins_url
response = requests.get(url=url, headers=headers)
if response.status_code == 200:
return response.json().get('candles')
else:
return None

Another thing which can should do is check for the status 200.

connectionreseterror: (errno 104) connection reset by peer

SOlved.
I was using JQuery autocomplete to search data from backend and wanted to implement onclick functionality that redirects the user to that specific item page.

$(document).ready(function() {
$("#search").autocomplete({
source: "{% url 'ajax-search' %}",
select: function (event, ui) {
window.location.href = ui.item.value;
}
})
window.location.href = ''
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div class="search">
<label for="search"></label><input type="text" oninput="" style="height: 36px" class="searchTerm"
placeholder="What are you looking for?" name="searchtext"
id="search"><input type="hidden" id="project-id">
<button type="submit" class="searchButton">
<i class="fa fa-search"></i>
</button>
</div>


Related Topics



Leave a reply



Submit