use requests if available
This commit is contained in:
parent
fa7f83e8c6
commit
ce50a626dc
1 changed files with 28 additions and 12 deletions
16
ox/cache.py
16
ox/cache.py
|
@ -13,6 +13,11 @@ import zlib
|
||||||
from six import BytesIO
|
from six import BytesIO
|
||||||
from six.moves import urllib
|
from six.moves import urllib
|
||||||
from six import PY2
|
from six import PY2
|
||||||
|
try:
|
||||||
|
import requests
|
||||||
|
USE_REQUESTS = True
|
||||||
|
except:
|
||||||
|
USE_REQUESTS = False
|
||||||
|
|
||||||
from .utils import json
|
from .utils import json
|
||||||
from .file import makedirs
|
from .file import makedirs
|
||||||
|
@ -94,6 +99,17 @@ def read_url(url, data=None, headers=None, timeout=cache_timeout, valid=None, un
|
||||||
result = store.get(url, data, headers, timeout)
|
result = store.get(url, data, headers, timeout)
|
||||||
url_headers = {}
|
url_headers = {}
|
||||||
if not result:
|
if not result:
|
||||||
|
if USE_REQUESTS:
|
||||||
|
r = requests.get(url, headers=headers)
|
||||||
|
for key in r.headers:
|
||||||
|
url_headers[key.lower()] = r.headers[key]
|
||||||
|
result = r.content
|
||||||
|
url_headers['Status'] = "%s" % r.status_code
|
||||||
|
if not valid or valid(result, url_headers):
|
||||||
|
store.set(url, post_data=data, data=result, headers=url_headers)
|
||||||
|
else:
|
||||||
|
raise InvalidResult(result, url_headers)
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
url_headers, result = net.read_url(url, data, headers, return_headers=True)
|
url_headers, result = net.read_url(url, data, headers, return_headers=True)
|
||||||
except urllib.error.HTTPError as e:
|
except urllib.error.HTTPError as e:
|
||||||
|
|
Loading…
Reference in a new issue