def _handle_response(self, req, uri, arg_data, _timeout=None):
kwargs = {}
if _timeout:
kwargs['timeout'] = _timeout
try:
handle = urllib_request.urlopen(req, **kwargs)
if handle.headers['Content-Type'] in ['image/jpeg', 'image/png', 'image/gif']:
print(handle.headers['Content-Type'])
return handle
try:
data = handle.read()
except http_client.IncompleteRead as e:
# Even if we don't get all the bytes we should have there
# may be a complete response in e.partial
data = e.partial
if handle.info().get('Content-Encoding') == 'gzip':
# Handle gzip decompression.
buf = BytesIO(data)
f = gzip.GzipFile(fileobj=buf)
data = f.read()
if len(data) == 0:
return wrap_response({}, handle.headers)
elif 'json' == self.format:
res = json.loads(data.decode('utf-8'))
return wrap_response(res, handle.headers)
else:
return wrap_response(
data.decode('utf-8'), handle.headers)
except urllib_error.HTTPError as e:
if (e.code == 304):
return []
else:
raise FanfouHTTPError(e, uri, self.format, arg_data)
评论列表
文章目录