Mercurial > public > sg101
comparison core/download.py @ 980:3ebde23a59d0
Pass timeout to requests instead of global timeout.
Global timeout didn't seem to work once we started using requests.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Thu, 15 Oct 2015 18:35:56 -0500 |
parents | a6331579ff43 |
children | ef1558941bc9 |
comparison
equal
deleted
inserted
replaced
979:a6331579ff43 | 980:3ebde23a59d0 |
---|---|
10 | 10 |
11 | 11 |
12 logger = logging.getLogger(__name__) | 12 logger = logging.getLogger(__name__) |
13 | 13 |
14 | 14 |
15 def download_file(url, path=None): | 15 def download_file(url, path=None, timeout=None): |
16 """Downloads the image file from the given source URL and stores it in the | 16 """Downloads the image file from the given source URL and stores it in the |
17 filename given by path. If path is None, a temporary file will be created. | 17 filename given by path. If path is None, a temporary file will be created. |
18 | 18 |
19 If successful returns the path to the downloaded file. Otherwise None is | 19 If successful returns the path to the downloaded file. Otherwise None is |
20 returned. | 20 returned. |
22 This function may raise various exceptions from the requests library. | 22 This function may raise various exceptions from the requests library. |
23 """ | 23 """ |
24 logger.info("download_file from %s; path=%s", url, path) | 24 logger.info("download_file from %s; path=%s", url, path) |
25 | 25 |
26 try: | 26 try: |
27 r = requests.get(url, stream=True) | 27 r = requests.get(url, stream=True, timeout=timeout) |
28 except requests.RequestException: | 28 except requests.RequestException: |
29 logger.exception("download_file requests.get('%s') exception", url) | 29 logger.exception("download_file requests.get('%s') exception", url) |
30 raise | 30 raise |
31 | 31 |
32 if r.status_code != 200: | 32 if r.status_code != 200: |