Merge pull request #1425 from Lukasa/stream
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Use new urllib3 'stream' parameter.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 25db83f, 1faa76a · Browse files · 3 files·+50 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+50 added−6 removed
- other:+50 added−6 removed
Provenance
- Commit
3781c96- Tree
fd1810f- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
25db83f- Parent 2
1faa76a
3 files changed+50 added−6 removed
modified:requests/packages/urllib3/response.py+24 added
9 unmodified lines10 import io11 12 from .exceptions import DecodeError13 from .packages.six import string_types as basestring, binary_type14+from .util import is_fp_closed15 16 17 log = logging.getLogger(__name__)18 182 unmodified lines201 finally:202 if self._original_response and self._original_response.isclosed():203 self.release_conn()204 205+ def stream(self, amt=2**16, decode_content=None):206+ """207+ A generator wrapper for the read() method. A call will block until208+ ``amt`` bytes have been read from the connection or until the209+ connection is closed.210+211+ :param amt:212+ How much of the content to read. The generator will return up to213+ much data per iteration, but may return less. This is particularly214+ likely when using compressed data. However, the empty string will215+ never be returned.216+217+ :param decode_content:218+ If True, will attempt to decode the body based on the219+ 'content-encoding' header.220+ """221+ while not is_fp_closed(self._fp):222+ data = self.read(amt=amt, decode_content=decode_content)223+224+ if data:225+ yield data226+227+228 @classmethod229 def from_httplib(ResponseCls, r, **response_kw):230 """231 Given an :class:`httplib.HTTPResponse` instance ``r``, return a67 unmodified linesmodified:requests/packages/urllib3/util.py+14 added-1 removed
29 unmodified lines30 from ssl import HAS_SNI # Has SNI?31 except ImportError:32 pass33 34-34 from .packages import six35 from .exceptions import LocationParseError, SSLError36 37 301 unmodified lines339 raise SSLError('Fingerprints did not match. Expected "{0}", got "{1}".'340 .format(hexlify(fingerprint_bytes),341 hexlify(cert_digest)))342 343+def is_fp_closed(obj):344+ """345+ Checks whether a given file-like object is closed.346 347+ :param obj:348+ The file-like object to check.349+ """350+ if hasattr(obj, 'fp'):351+ # Object is a container for another file-like object that gets released352+ # on exhaustion (e.g. HTTPResponse)353+ return obj.fp is None354+355+ return obj.closed356+357+358 if SSLContext is not None: # Python 3.2+359 def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=None,360 ca_certs=None, server_hostname=None,361 ssl_version=None):30 unmodified linesmodified:requests/models.py+12 added-5 removed
535 unmodified lines536 # simulate reading small chunks of the content537 return iter_slices(self._content, chunk_size)538 539 def generate():540+ try:541+ # Special case for urllib3.542+ for chunk in self.raw.stream(chunk_size, decode_content=True):543+ yield chunk544+ except AttributeError:545+ # Standard file-like object.540- while 1:541- chunk = self.raw.read(chunk_size, decode_content=True)542- if not chunk:543- break544- yield chunk546+ while 1:547+ chunk = self.raw.read(chunk_size, decode_content=True)548+ if not chunk:549+ break550+ yield chunk551+552 self._content_consumed = True553 554 gen = generate()555 138 unmodified lines