use requests if available

This commit is contained in:
j 2016-06-16 15:30:27 +02:00
parent fa7f83e8c6
commit ce50a626dc

View file

@ -13,6 +13,11 @@ import zlib
from six import BytesIO
from six.moves import urllib
from six import PY2
try:
import requests
USE_REQUESTS = True
except:
USE_REQUESTS = False
from .utils import json
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)
url_headers = {}
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:
url_headers, result = net.read_url(url, data, headers, return_headers=True)
except urllib.error.HTTPError as e: