Handling ConnectionResetError
<p>I have a question on handling ConnectionResetError in Python3. This usually happens when I use the urllib.request.Request function. I would like to know if it is ok to redo the request if we come across such error. For example</p>

<pre><code>def get_html(url):
    try:
        request = Request(url)
        response = urlopen(request)
        html = response.read()
    except ConectionReserError as e:
        get_html(url)
</code></pre>

<p>It really depends on the server, but you could do something like:</p>

<pre><code>def get_html(url, retry_count=0):
    try:
        request = Request(url)
        response = urlopen(request)
        html = response.read()
    except ConectionResetError as e:
        if retry_count == MAX_RETRIES:
            raise e
        time.sleep(for_some_time)
        get_html(url, retry_count + 1)
</code></pre>

<p>Also see <a href="http://stackoverflow.com/questions/20568216/python-handling-socket-error-errno-104-connection-reset-by-peer">Python handling socket.error: [Errno 104] Connection reset by peer</a></p>

python urllib