mirror of
https://github.com/soimort/you-get.git
synced 2025-01-24 22:15:03 +03:00
fix urlopen_with_retry may return None
This commit is contained in:
parent
c2de0bdc38
commit
e88364bf56
@ -342,14 +342,19 @@ def get_location(url):
|
|||||||
return response.geturl()
|
return response.geturl()
|
||||||
|
|
||||||
def urlopen_with_retry(*args, **kwargs):
|
def urlopen_with_retry(*args, **kwargs):
|
||||||
for i in range(2):
|
retry_time = 3
|
||||||
|
for i in range(retry_time):
|
||||||
try:
|
try:
|
||||||
return request.urlopen(*args, **kwargs)
|
return request.urlopen(*args, **kwargs)
|
||||||
except socket.timeout:
|
except socket.timeout as e:
|
||||||
logging.debug('request attempt %s timeout' % str(i + 1))
|
logging.debug('request attempt %s timeout' % str(i + 1))
|
||||||
|
if i + 1 == retry_time:
|
||||||
|
raise e
|
||||||
# try to tackle youku CDN fails
|
# try to tackle youku CDN fails
|
||||||
except error.HTTPError as http_error:
|
except error.HTTPError as http_error:
|
||||||
logging.debug('HTTP Error with code{}'.format(http_error.code))
|
logging.debug('HTTP Error with code{}'.format(http_error.code))
|
||||||
|
if i + 1 == retry_time:
|
||||||
|
raise http_error
|
||||||
|
|
||||||
def get_content(url, headers={}, decoded=True):
|
def get_content(url, headers={}, decoded=True):
|
||||||
"""Gets the content of a URL via sending a HTTP GET request.
|
"""Gets the content of a URL via sending a HTTP GET request.
|
||||||
|
Loading…
Reference in New Issue
Block a user