title: Recovering from ECONNRESET in Python/Mechanize
body: <p>I've got a large bulk downloading application written in Python/Mechanize, aiming to download something like 20,000 files.  Clearly, any downloader that big is occasionally going to run into some <code>ECONNRESET</code> errors.  Now, I know how to handle <a href="https://stackoverflow.com/questions/4605929/retrying-on-connection-reset">each of these individually</a>, but there's two problems with that:</p>
  
  <ol>
  <li>I'd really rather not wrap every single outbound web call in a try/catch block.</li>
  <li><p>Even if I were to do so, there's trouble with knowing how to handle the errors once the exception has thrown.  If the code is just</p>
  
  <pre><code>data = browser.response().read()
  </code></pre>
  
  <p>then I know precisely how to deal with it, namely:</p>
  
  <pre><code>data = None
  while (data == None):
      try:
          data = browser.response().read()
      except IOError as e:
          if e.args[1].args[0].errno != errno.ECONNRESET:
              raise
          data = None
  </code></pre>
  
  <p>but if it's just a random instance of</p>
  
  <pre><code>browser.follow_link(link)
  </code></pre>
  
  <p>then how do I know what Mechanize's internal state looks like if an <code>ECONNRESET</code> is thrown somewhere in here?  For example, do I need to call <code>browser.back()</code> before I try the code again?  What's the proper way to recover from that kind of error?</p></li>
  </ol>
  
  <p>EDIT: The solution in the accepted answer certainly works, and in my case it turned out to be not so hard to implement.  I'm still academically interested, however, in whether there's an error handling mechanism that could result in quicker error catching.</p>