Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retry urllib2.request when fails?

When urllib2.request reaches timeout, a urllib2.URLError exception is raised. What is the pythonic way to retry establishing a connection?

like image 582
iTayb Avatar asked Feb 25 '12 17:02

iTayb


People also ask

What does Urllib request return?

This function always returns an object which can work as a context manager and has the properties url, headers, and status. See urllib.

What does Urllib Urlopen return?

The data returned by urlopen() or urlretrieve() is the raw data returned by the server. This may be binary data (such as an image), plain text or (for example) HTML. The HTTP protocol provides type information in the reply header, which can be inspected by looking at the Content-Type header.

Can I use urllib2 in python3?

NOTE: urllib2 is no longer available in Python 3 You can get more idea about urllib.

What does urllib2 do in Python?

Urllib package is the URL handling module for python. It is used to fetch URLs (Uniform Resource Locators). It uses the urlopen function and is able to fetch URLs using a variety of different protocols.


2 Answers

There are a few libraries out there that specialize in this.

One is backoff, which is designed with a particularly functional sensibility. Decorators are passed arbitrary callables returning generators which yield successive delay values. A simple exponential backoff with a maximum retry time of 32 seconds could be defined as:

@backoff.on_exception(backoff.expo,                       urllib2.URLError,                       max_value=32) def url_open(url):     return urllib2.urlopen("http://example.com") 

Another is retrying which has very similar functionality but an API where retry parameters are specified by way of predefined keyword args.

like image 39
bgreen-litl Avatar answered Sep 20 '22 13:09

bgreen-litl


I would use a retry decorator. There are other ones out there, but this one works pretty well. Here's how you can use it:

@retry(urllib2.URLError, tries=4, delay=3, backoff=2) def urlopen_with_retry():     return urllib2.urlopen("http://example.com") 

This will retry the function if URLError is raised. Check the link above for documentation on the parameters, but basically it will retry a maximum of 4 times, with an exponential backoff delay doubling each time, e.g. 3 seconds, 6 seconds, 12 seconds.

like image 126
jterrace Avatar answered Sep 19 '22 13:09

jterrace