+ handle = urllib_request.urlopen(req, **kwargs)
+ if handle.headers['Content-Type'] in ['image/jpeg', 'image/png']:
+ 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 = StringIO(data)
+ f = gzip.GzipFile(fileobj=buf)
+ data = f.read()