• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python response.HTTPResponse类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: test_empty_stream

    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,代码行数:7,代码来源:test_response.py


示例2: test_io

    def test_io(self):
        import socket
        try:
            from http.client import HTTPResponse as OldHTTPResponse
        except:
            from httplib import HTTPResponse as OldHTTPResponse

        fp = BytesIO(b'foo')
        resp = HTTPResponse(fp, preload_content=False)

        self.assertEqual(resp.closed, False)
        self.assertEqual(resp.readable(), True)
        self.assertEqual(resp.writable(), False)
        self.assertRaises(IOError, resp.fileno)

        resp.close()
        self.assertEqual(resp.closed, True)

        # Try closing with an `httplib.HTTPResponse`, because it has an
        # `isclosed` method.
        hlr = OldHTTPResponse(socket.socket())
        resp2 = HTTPResponse(hlr, preload_content=False)
        self.assertEqual(resp2.closed, False)
        resp2.close()
        self.assertEqual(resp2.closed, True)

        #also try when only data is present.
        resp3 = HTTPResponse('foodata')
        self.assertRaises(IOError, resp3.fileno)

        resp3._fp = 2
        # A corner case where _fp is present but doesn't have `closed`,
        # `isclosed`, or `fileno`.  Unlikely, but possible.
        self.assertEqual(resp3.closed, True)
        self.assertRaises(IOError, resp3.fileno)
开发者ID:Mofangbao,项目名称:urllib3,代码行数:35,代码来源:test_response.py


示例3: test_mock_httpresponse_stream

    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,代码行数:26,代码来源:test_response.py


示例4: test_reference_read

    def test_reference_read(self):
        fp = BytesIO(b'foo')
        r = HTTPResponse(fp, preload_content=False)

        self.assertEqual(r.read(1), b'f')
        self.assertEqual(r.read(2), b'oo')
        self.assertEqual(r.read(), b'')
        self.assertEqual(r.read(), b'')
开发者ID:Lukasa,项目名称:urllib3,代码行数:8,代码来源:test_response.py


示例5: test_reference_read

    def test_reference_read(self):
        fp = BytesIO(b'foo')
        r = HTTPResponse(fp, preload_content=False)

        assert r.read(1) == b'f'
        assert r.read(2) == b'oo'
        assert r.read() == b''
        assert r.read() == b''
开发者ID:jonparrott,项目名称:urllib3,代码行数:8,代码来源:test_response.py


示例6: test_reference_read

    def test_reference_read(self):
        fp = BytesIO(b"foo")
        r = HTTPResponse(fp, preload_content=False)

        self.assertEqual(r.read(1), b"f")
        self.assertEqual(r.read(2), b"oo")
        self.assertEqual(r.read(), b"")
        self.assertEqual(r.read(), b"")
开发者ID:balagopalraj,项目名称:clearlinux,代码行数:8,代码来源:test_response.py


示例7: test_streaming

    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,代码行数:8,代码来源:test_response.py


示例8: test_streaming

    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,代码行数:9,代码来源:test_response.py


示例9: test_invalid_chunks

 def test_invalid_chunks(self):
     stream = [b"foooo", b"bbbbaaaaar"]
     fp = MockChunkedInvalidEncoding(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'})
     self.assertRaises(ProtocolError, next, resp.read_chunked())
开发者ID:Lukasa,项目名称:urllib3,代码行数:9,代码来源:test_response.py


示例10: test_chunked_response_with_extensions

 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,代码行数:9,代码来源:test_response.py


示例11: test_mock_transfer_encoding_chunked_unlmtd_read

 def test_mock_transfer_encoding_chunked_unlmtd_read(self):
     stream = [b"foooo", b"bbbbaaaaar"]
     fp = MockChunkedEncodingResponse(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.read_chunked())
开发者ID:jonparrott,项目名称:urllib3,代码行数:9,代码来源:test_response.py


示例12: test_mock_transfer_encoding_chunked

    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,代码行数:9,代码来源:test_response.py


示例13: test_chunked_decoding_deflate

    def test_chunked_decoding_deflate(self):
        import zlib
        data = zlib.compress(b'foo')

        fp = BytesIO(data)
        r = HTTPResponse(fp, headers={'content-encoding': 'deflate'},
                         preload_content=False)

        self.assertEqual(r.read(3), b'')
        self.assertEqual(r.read(1), b'f')
        self.assertEqual(r.read(2), b'oo')
开发者ID:Mofangbao,项目名称:urllib3,代码行数:11,代码来源:test_response.py


示例14: test_mock_transfer_encoding_chunked

    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,代码行数:11,代码来源:test_response.py


示例15: test_geturl_retries

 def test_geturl_retries(self):
     fp = BytesIO(b'')
     resp = HTTPResponse(fp, request_url='http://example.com')
     request_histories = [
         RequestHistory(method='GET', url='http://example.com', error=None,
                        status=301, redirect_location='https://example.com/'),
         RequestHistory(method='GET', url='https://example.com/', error=None,
                        status=301, redirect_location='https://www.example.com')]
     retry = Retry(history=request_histories)
     resp = HTTPResponse(fp, retries=retry)
     assert resp.geturl() == 'https://www.example.com'
开发者ID:jonparrott,项目名称:urllib3,代码行数:11,代码来源:test_response.py


示例16: test_chunked_decoding_deflate

    def test_chunked_decoding_deflate(self):
        import zlib

        data = zlib.compress(b"foo")

        fp = BytesIO(data)
        r = HTTPResponse(fp, headers={"content-encoding": "deflate"}, preload_content=False)

        self.assertEqual(r.read(3), b"")
        self.assertEqual(r.read(1), b"f")
        self.assertEqual(r.read(2), b"oo")
开发者ID:github3py,项目名称:urllib3,代码行数:11,代码来源:test_response.py


示例17: test_mock_transfer_encoding_chunked_custom_read

 def test_mock_transfer_encoding_chunked_custom_read(self):
     stream = [b"foooo", b"bbbbaaaaar"]
     fp = MockChunkedEncodingResponse(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'})
     expected_response = [b'fo', b'oo', b'o', b'bb', b'bb', b'aa', b'aa', b'ar']
     response = list(resp.read_chunked(2))
     assert expected_response == response
开发者ID:jonparrott,项目名称:urllib3,代码行数:11,代码来源:test_response.py


示例18: test_deflate_streaming

    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,代码行数:12,代码来源:test_response.py


示例19: test_deflate_streaming

    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,代码行数:12,代码来源:test_response.py


示例20: test_deflate_streaming_tell_intermediate_point

    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,代码行数:52,代码来源:test_response.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python util.parse_url函数代码示例发布时间:2022-05-27
下一篇:
Python poolmanager.PoolManager类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap