-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathcodecomments3.py
38 lines (31 loc) · 1.25 KB
/
codecomments3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Code listing #5
def fetch_url(url, ntries=3, timeout=30):
""" Fetch a given url and return its contents.
@params
url - The URL to be fetched.
ntries - The maximum number of retries.
timeout - Timout per call in seconds.
@returns
On success - Contents of URL.
On failure - (error_code, last_error)
"""
# This loop performs a network fetch of the URL, retrying upto
# 'ntries' times in case of errors. In case the URL cant be
# fetched, an error is returned.
# Initialize all state
count, result, error = 0, None, None
while count < ntries:
try:
result = requests.get(url, timeout=timeout)
except Exception as error:
print('Caught exception', error, 'trying again after a while')
# increment count
count += 1
# sleep 1 second every time
time.sleep(1)
if result == None:
print("Error, could not fetch URL",url)
# Return a tuple of (<return code>, <lasterror>)
return (2, error)
# Return data of the URL
return result.content