当前位置: 首页>>代码示例>>Python>>正文


Python HTTPResponse.stream方法代码示例

本文整理汇总了Python中urllib3.response.HTTPResponse.stream方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPResponse.stream方法的具体用法?Python HTTPResponse.stream怎么用?Python HTTPResponse.stream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在urllib3.response.HTTPResponse的用法示例。


在下文中一共展示了HTTPResponse.stream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_chunked_response_with_extensions

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
 def test_chunked_response_with_extensions(self):
     stream = [b"foo", b"bar"]
     fp = MockChunkedEncodingWithExtensions(stream)
     r = httplib.HTTPResponse(MockSock)
     r.fp = fp
     r.chunked = True
     r.chunk_left = None
     resp = HTTPResponse(r, preload_content=False, headers={'transfer-encoding': 'chunked'})
     if getattr(self, "assertListEqual", False):
         self.assertListEqual(stream, list(resp.stream()))
     else:
         for index, item in enumerate(resp.stream()):
             v = stream[index]
             self.assertEqual(item, v)
开发者ID:Lukasa,项目名称:urllib3,代码行数:16,代码来源:test_response.py

示例2: test_mock_httpresponse_stream

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_mock_httpresponse_stream(self):
        # Mock out a HTTP Request that does enough to make it through urllib3's
        # read() and close() calls, and also exhausts and underlying file
        # object.
        class MockHTTPRequest(object):
            self.fp = None

            def read(self, amt):
                data = self.fp.read(amt)
                if not data:
                    self.fp = None

                return data

            def close(self):
                self.fp = None

        bio = BytesIO(b'foo')
        fp = MockHTTPRequest()
        fp.fp = bio
        resp = HTTPResponse(fp, preload_content=False)
        stream = resp.stream(2)

        self.assertEqual(next(stream), b'fo')
        self.assertEqual(next(stream), b'o')
        self.assertRaises(StopIteration, next, stream)
开发者ID:Lukasa,项目名称:urllib3,代码行数:28,代码来源:test_response.py

示例3: test_empty_stream

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_empty_stream(self):
        fp = BytesIO(b'')
        resp = HTTPResponse(fp, preload_content=False)
        stream = resp.stream(2, decode_content=False)

        with pytest.raises(StopIteration):
            next(stream)
开发者ID:jonparrott,项目名称:urllib3,代码行数:9,代码来源:test_response.py

示例4: test_streaming

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_streaming(self):
        fp = BytesIO(b'foo')
        resp = HTTPResponse(fp, preload_content=False)
        stream = resp.stream(2, decode_content=False)

        self.assertEqual(next(stream), b'fo')
        self.assertEqual(next(stream), b'o')
        self.assertRaises(StopIteration, next, stream)
开发者ID:Lukasa,项目名称:urllib3,代码行数:10,代码来源:test_response.py

示例5: test_streaming

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_streaming(self):
        fp = BytesIO(b'foo')
        resp = HTTPResponse(fp, preload_content=False)
        stream = resp.stream(2, decode_content=False)

        assert next(stream) == b'fo'
        assert next(stream) == b'o'
        with pytest.raises(StopIteration):
            next(stream)
开发者ID:jonparrott,项目名称:urllib3,代码行数:11,代码来源:test_response.py

示例6: test_chunked_response_with_extensions

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
 def test_chunked_response_with_extensions(self):
     stream = [b"foo", b"bar"]
     fp = MockChunkedEncodingWithExtensions(stream)
     r = httplib.HTTPResponse(MockSock)
     r.fp = fp
     r.chunked = True
     r.chunk_left = None
     resp = HTTPResponse(r, preload_content=False, headers={'transfer-encoding': 'chunked'})
     assert stream == list(resp.stream())
开发者ID:jonparrott,项目名称:urllib3,代码行数:11,代码来源:test_response.py

示例7: test_mock_transfer_encoding_chunked

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_mock_transfer_encoding_chunked(self):
        stream = [b"fo", b"o", b"bar"]
        fp = MockChunkedEncodingResponse(stream)
        r = httplib.HTTPResponse(MockSock)
        r.fp = fp
        resp = HTTPResponse(r, preload_content=False, headers={'transfer-encoding': 'chunked'})

        for i, c in enumerate(resp.stream()):
            assert c == stream[i]
开发者ID:jonparrott,项目名称:urllib3,代码行数:11,代码来源:test_response.py

示例8: test_mock_transfer_encoding_chunked

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_mock_transfer_encoding_chunked(self):
        stream = [b"fo", b"o", b"bar"]
        fp = MockChunkedEncodingResponse(stream)
        r = httplib.HTTPResponse(MockSock)
        r.fp = fp
        resp = HTTPResponse(r, preload_content=False, headers={'transfer-encoding': 'chunked'})

        i = 0
        for c in resp.stream():
            self.assertEqual(c, stream[i])
            i += 1
开发者ID:Lukasa,项目名称:urllib3,代码行数:13,代码来源:test_response.py

示例9: test_deflate_streaming

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_deflate_streaming(self):
        import zlib
        data = zlib.compress(b'foo')

        fp = BytesIO(data)
        resp = HTTPResponse(fp, headers={'content-encoding': 'deflate'},
                            preload_content=False)
        stream = resp.stream(2)

        self.assertEqual(next(stream), b'f')
        self.assertEqual(next(stream), b'oo')
        self.assertRaises(StopIteration, next, stream)
开发者ID:Lukasa,项目名称:urllib3,代码行数:14,代码来源:test_response.py

示例10: test_deflate_streaming

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_deflate_streaming(self):
        data = zlib.compress(b'foo')

        fp = BytesIO(data)
        resp = HTTPResponse(fp, headers={'content-encoding': 'deflate'},
                            preload_content=False)
        stream = resp.stream(2)

        assert next(stream) == b'f'
        assert next(stream) == b'oo'
        with pytest.raises(StopIteration):
            next(stream)
开发者ID:jonparrott,项目名称:urllib3,代码行数:14,代码来源:test_response.py

示例11: test_deflate_streaming_tell_intermediate_point

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_deflate_streaming_tell_intermediate_point(self):
        # Ensure that ``tell()`` returns the correct number of bytes when
        # part-way through streaming compressed content.
        import zlib

        NUMBER_OF_READS = 10

        class MockCompressedDataReading(BytesIO):
            """
            A ByteIO-like reader returning ``payload`` in ``NUMBER_OF_READS``
            calls to ``read``.
            """

            def __init__(self, payload, payload_part_size):
                self.payloads = [
                    payload[i*payload_part_size:(i+1)*payload_part_size]
                    for i in range(NUMBER_OF_READS+1)]

                assert b"".join(self.payloads) == payload

            def read(self, _):
                # Amount is unused.
                if len(self.payloads) > 0:
                    return self.payloads.pop(0)
                return b""

        uncompressed_data = zlib.decompress(ZLIB_PAYLOAD)

        payload_part_size = len(ZLIB_PAYLOAD) // NUMBER_OF_READS
        fp = MockCompressedDataReading(ZLIB_PAYLOAD, payload_part_size)
        resp = HTTPResponse(fp, headers={'content-encoding': 'deflate'},
                            preload_content=False)
        stream = resp.stream()

        parts_positions = [(part, resp.tell()) for part in stream]
        end_of_stream = resp.tell()

        with pytest.raises(StopIteration):
            next(stream)

        parts, positions = zip(*parts_positions)

        # Check that the payload is equal to the uncompressed data
        payload = b"".join(parts)
        assert uncompressed_data == payload

        # Check that the positions in the stream are correct
        expected = [(i+1)*payload_part_size for i in range(NUMBER_OF_READS)]
        assert expected == list(positions)

        # Check that the end of the stream is in the correct place
        assert len(ZLIB_PAYLOAD) == end_of_stream
开发者ID:nateprewitt,项目名称:urllib3,代码行数:54,代码来源:test_response.py

示例12: test_gzipped_streaming

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_gzipped_streaming(self):
        import zlib

        compress = zlib.compressobj(6, zlib.DEFLATED, 16 + zlib.MAX_WBITS)
        data = compress.compress(b"foo")
        data += compress.flush()

        fp = BytesIO(data)
        resp = HTTPResponse(fp, headers={"content-encoding": "gzip"}, preload_content=False)
        stream = resp.stream(2)

        self.assertEqual(next(stream), b"f")
        self.assertEqual(next(stream), b"oo")
        self.assertRaises(StopIteration, next, stream)
开发者ID:balagopalraj,项目名称:clearlinux,代码行数:16,代码来源:test_response.py

示例13: test_deflate2_streaming

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_deflate2_streaming(self):
        import zlib
        compress = zlib.compressobj(6, zlib.DEFLATED, -zlib.MAX_WBITS)
        data = compress.compress(b'foo')
        data += compress.flush()

        fp = BytesIO(data)
        resp = HTTPResponse(fp, headers={'content-encoding': 'deflate'},
                            preload_content=False)
        stream = resp.stream(2)

        self.assertEqual(next(stream), b'f')
        self.assertEqual(next(stream), b'oo')
        self.assertRaises(StopIteration, next, stream)
开发者ID:Lukasa,项目名称:urllib3,代码行数:16,代码来源:test_response.py

示例14: test_deflate2_streaming

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_deflate2_streaming(self):
        compress = zlib.compressobj(6, zlib.DEFLATED, -zlib.MAX_WBITS)
        data = compress.compress(b'foo')
        data += compress.flush()

        fp = BytesIO(data)
        resp = HTTPResponse(fp, headers={'content-encoding': 'deflate'},
                            preload_content=False)
        stream = resp.stream(2)

        assert next(stream) == b'f'
        assert next(stream) == b'oo'
        with pytest.raises(StopIteration):
            next(stream)
开发者ID:jonparrott,项目名称:urllib3,代码行数:16,代码来源:test_response.py

示例15: test_chunked_head_response

# 需要导入模块: from urllib3.response import HTTPResponse [as 别名]
# 或者: from urllib3.response.HTTPResponse import stream [as 别名]
    def test_chunked_head_response(self):
        r = httplib.HTTPResponse(MockSock, method='HEAD')
        r.chunked = True
        r.chunk_left = None
        resp = HTTPResponse('',
                            preload_content=False,
                            headers={'transfer-encoding': 'chunked'},
                            original_response=r)
        assert resp.chunked is True

        resp.supports_chunked_reads = lambda: True
        resp.release_conn = mock.Mock()
        for _ in resp.stream():
            continue
        resp.release_conn.assert_called_once_with()
开发者ID:jonparrott,项目名称:urllib3,代码行数:17,代码来源:test_response.py


注:本文中的urllib3.response.HTTPResponse.stream方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。