How to send a repeat request in urllib2.urlopen in Python if the first call is just stuck?
<p>I am making a call to a URL in Python using urllib2.urlopen in a while(True) loop</p>

<p>My URL keeps changing every time (as there is a change in a particular parameter of the URL every time).</p>

<p>My code look as as follows:</p>

<pre><code>def get_url(url):
    '''Get json page data using a specified API url'''
    response = urlopen(url)
    data = str(response.read().decode('utf-8'))
    page = json.loads(data)
    return page
</code></pre>

<p>I am calling the above method from the main function by changing the url every time I make the call.</p>

<p>What I observe is that after few calls to the function, suddenly (I don;t know why), the code gets stuck at the statement</p>

<pre><code>response = urlopen(url)
</code></pre>

<p>and it just waits and waits...</p>

<p>How do I best handle this situation?</p>

<p>I want to make sure that if it does not respond within say 10 seconds, I make the same call again.</p>

<p>I read about </p>

<pre><code>response = urlopen(url, timeout=10)
</code></pre>

<p>but then what about the repeated call if this fails?</p>

<p>With this method you can retry once. </p>

<pre><code>def get_url(url, trial=1):
    try:    
        '''Get json page data using a specified API url'''
        response = urlopen(url, timeout=10)
        data = str(response.read().decode('utf-8'))
        page = json.loads(data)
        return page
    except:
        if trial == 1:
            return get_url(url, trial=2)
        else:
            return
</code></pre>

python http urllib2