Python urllib3.response.HTTPResponse() Examples

The following are 10 code examples of urllib3.response.HTTPResponse(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urllib3.response , or try the search function .
Example #1
Source File: responses.py    From jarvis with GNU General Public License v2.0 7 votes vote down vote up
def get_response(self, request):
        headers = self.get_headers()

        result = self.callback(request)
        if isinstance(result, Exception):
            raise result

        status, r_headers, body = result
        body = _handle_body(body)
        headers.update(r_headers)

        return HTTPResponse(
            status=status,
            reason=six.moves.http_client.responses.get(status),
            body=body,
            headers=headers,
            preload_content=False, ) 
Example #2
Source File: test_query.py    From gsheets-db-api with MIT License 6 votes vote down vote up
def test_run_query_no_encoding(self, m):
        raw = HTTPResponse(
            body=BytesIO('"ok"'.encode('utf-8')),
            preload_content=False,
            headers={
                'Content-type': 'application/json',
            },
            status=200,
        )
        m.get('http://docs.google.com/&tq=SELECT%20%2A', raw=raw)

        baseurl = 'http://docs.google.com/'
        query = 'SELECT *'
        result = run_query(baseurl, query)
        expected = 'ok'
        self.assertEqual(result, expected) 
Example #3
Source File: test_negotiate.py    From pypsrp with MIT License 5 votes vote down vote up
def test_get_cbt_fail_invalid_raw_socket(self):
        response = MagicMock()
        response.raw = HTTPResponse()

        expected_warning = "Failed to get raw socket for CBT from urllib3 resp"

        with warnings.catch_warnings(record=True) as w:
            actual = HTTPNegotiateAuth._get_cbt_data(response)
            assert actual is None
            assert expected_warning in str(w[-1].message) 
Example #4
Source File: test_negotiate.py    From pypsrp with MIT License 5 votes vote down vote up
def test_get_cbt_no_peer_cert(self):
        mock_socket = MagicMock()
        mock_socket.getpeercert.side_effect = AttributeError

        raw_response = HTTPResponse()
        raw_response._fp = MagicMock()
        raw_response._fp.fp.raw._sock = mock_socket
        raw_response._fp.fp._sock = mock_socket

        response = MagicMock()
        response.raw = raw_response

        actual = HTTPNegotiateAuth._get_cbt_data(response)
        assert actual is None 
Example #5
Source File: test_negotiate.py    From pypsrp with MIT License 5 votes vote down vote up
def test_get_cbt_with_peer_cert(self):
        cert_der = b'MIIDGzCCAgOgAwIBAgIQWkeAtqoFg6pNWF7xC4YXhTANBgkqhkiG9w0' \
                   b'BAQsFADAVMRMwEQYDVQQDDApTRVJWRVIyMDE2MB4XDTE3MDUyNzA5MD' \
                   b'I0NFoXDTE4MDUyNzA5MjI0NFowFTETMBEGA1UEAwwKU0VSVkVSMjAxN' \
                   b'jCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALIPKM5uykFy' \
                   b'NmVoLyvPSXGk15ZDqjYi3AbUxVFwCkVImqhefLATit3PkTUYFtAT+TC' \
                   b'AwK2E4lOu1XHM+Tmp2KIOnq2oUR8qMEvfxYThEf1MHxkctFljFssZ9N' \
                   b'vASDD4lzw8r0Bhl+E5PhR22Eu1Wago5bvIldojkwG+WBxPQv3ZR546L' \
                   b'MUZNaBXC0RhuGj5w83lbVz75qM98wvv1ekfZYAP7lrVyHxqCTPDomEU' \
                   b'I45tQQZHCZl5nRx1fPCyyYfcfqvFlLWD4Q3PZAbnw6mi0MiWJbGYKME' \
                   b'1XGicjqyn/zM9XKA1t/JzChS2bxf6rsyA9I7ibdRHUxsm1JgKry2jfW' \
                   b'0CAwEAAaNnMGUwDgYDVR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGA' \
                   b'QUFBwMCBggrBgEFBQcDATAVBgNVHREEDjAMggpTRVJWRVIyMDE2MB0G' \
                   b'A1UdDgQWBBQabLGWg1sn7AXPwYPyfE0ER921ZDANBgkqhkiG9w0BAQs' \
                   b'FAAOCAQEAnRohyl6ZmOsTWCtxOJx5A8yr//NweXKwWWmFQXRmCb4bMC' \
                   b'xhD4zqLDf5P6RotGV0I/SHvqz+pAtJuwmr+iyAF6WTzo3164LCfnQEu' \
                   b'psfrrfMkf3txgDwQkA0oPAw3HEwOnR+tzprw3Yg9x6UoZEhi4XqP9AX' \
                   b'R49jU92KrNXJcPlz5MbkzNo5t9nr2f8q39b5HBjaiBJxzdM1hxqsbfD' \
                   b'KirTYbkUgPlVOo/NDmopPPb8IX8ubj/XETZG2jixD0zahgcZ1vdr/iZ' \
                   b'+50WSXKN2TAKBO2fwoK+2/zIWrGRxJTARfQdF+fGKuj+AERIFNh88HW' \
                   b'xSDYjHQAaFMcfdUpa9GGQ=='
        cert_der = base64.b64decode(cert_der)

        mock_socket = MagicMock()
        mock_socket.getpeercert.return_value = cert_der

        raw_response = HTTPResponse()
        raw_response._fp = MagicMock()
        raw_response._fp.fp.raw._sock = mock_socket
        raw_response._fp.fp._sock = mock_socket

        response = MagicMock()
        response.raw = raw_response

        expected = b"tls-server-end-point:" \
                   b"\x99\x6F\x3E\xEA\x81\x2C\x18\x70\xE3\x05\x49\xFF" \
                   b"\x9B\x86\xCD\x87\xA8\x90\xB6\xD8\xDF\xDF\x4A\x81" \
                   b"\xBE\xF9\x67\x59\x70\xDA\xDB\x26"
        actual = HTTPNegotiateAuth._get_cbt_data(response)
        assert actual == expected 
Example #6
Source File: conftest.py    From openapi-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def response_factory():
    def create_response(
            data, status_code=200, content_type='application/json'):
        fp = BytesIO(b(data))
        raw = HTTPResponse(fp, preload_content=False)
        resp = Response()
        resp.headers = CaseInsensitiveDict({
            'Content-Type': content_type,
        })
        resp.status_code = status_code
        resp.raw = raw
        return resp
    return create_response 
Example #7
Source File: main.py    From AsyncLine with MIT License 5 votes vote down vote up
def save_file(self, path, raw):
		#TODO: Using Database for saving files
		with open(path, "wb") as f:
			if isinstance(raw, HTTPResponse):
				shutil.copyfileobj(raw, f)
			else:
				f.write(raw) 
Example #8
Source File: responses.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def get_response(self, request):
        if self.body and isinstance(self.body, Exception):
            raise self.body

        headers = self.get_headers()
        status = self.status
        body = _handle_body(self.body)

        return HTTPResponse(
            status=status,
            reason=six.moves.http_client.responses.get(status),
            body=body,
            headers=headers,
            preload_content=False, ) 
Example #9
Source File: contextmanagers.py    From pipenv with MIT License 4 votes vote down vote up
def open_file(
    link,  # type: Union[_T, str]
    session=None,  # type: Optional[Session]
    stream=True,  # type: bool
):
    # type: (...) -> ContextManager[Union[IO[bytes], Urllib3_HTTPResponse, Urllib_HTTPResponse]]
    """
    Open local or remote file for reading.

    :param pip._internal.index.Link link: A link object from resolving dependencies with
        pip, or else a URL.
    :param Optional[Session] session: A :class:`~requests.Session` instance
    :param bool stream: Whether to stream the content if remote, default True
    :raises ValueError: If link points to a local directory.
    :return: a context manager to the opened file-like object
    """
    if not isinstance(link, six.string_types):
        try:
            link = link.url_without_fragment
        except AttributeError:
            raise ValueError("Cannot parse url from unkown type: {0!r}".format(link))

    if not is_valid_url(link) and os.path.exists(link):
        link = path_to_url(link)

    if is_file_url(link):
        # Local URL
        local_path = url_to_path(link)
        if os.path.isdir(local_path):
            raise ValueError("Cannot open directory for read: {}".format(link))
        else:
            with io.open(local_path, "rb") as local_file:
                yield local_file
    else:
        # Remote URL
        headers = {"Accept-Encoding": "identity"}
        if not session:
            try:
                from requests import Session  # noqa
            except ImportError:
                session = None
            else:
                session = Session()
        if session is None:
            with closing(six.moves.urllib.request.urlopen(link)) as f:
                yield f
        else:
            with session.get(link, headers=headers, stream=stream) as resp:
                try:
                    raw = getattr(resp, "raw", None)
                    result = raw if raw else resp
                    yield result
                finally:
                    if raw:
                        conn = raw._connection
                        if conn is not None:
                            conn.close()
                    result.close() 
Example #10
Source File: contextmanagers.py    From vistir with ISC License 4 votes vote down vote up
def open_file(
    link,  # type: Union[_T, str]
    session=None,  # type: Optional[Session]
    stream=True,  # type: bool
):
    # type: (...) -> ContextManager[Union[IO[bytes], Urllib3_HTTPResponse, Urllib_HTTPResponse]]
    """
    Open local or remote file for reading.

    :param pip._internal.index.Link link: A link object from resolving dependencies with
        pip, or else a URL.
    :param Optional[Session] session: A :class:`~requests.Session` instance
    :param bool stream: Whether to stream the content if remote, default True
    :raises ValueError: If link points to a local directory.
    :return: a context manager to the opened file-like object
    """
    if not isinstance(link, six.string_types):
        try:
            link = link.url_without_fragment
        except AttributeError:
            raise ValueError("Cannot parse url from unkown type: {0!r}".format(link))

    if not is_valid_url(link) and os.path.exists(link):
        link = path_to_url(link)

    if is_file_url(link):
        # Local URL
        local_path = url_to_path(link)
        if os.path.isdir(local_path):
            raise ValueError("Cannot open directory for read: {}".format(link))
        else:
            with io.open(local_path, "rb") as local_file:
                yield local_file
    else:
        # Remote URL
        headers = {"Accept-Encoding": "identity"}
        if not session:
            try:
                from requests import Session  # noqa
            except ImportError:
                session = None
            else:
                session = Session()
        if session is None:
            with closing(six.moves.urllib.request.urlopen(link)) as f:
                yield f
        else:
            with session.get(link, headers=headers, stream=stream) as resp:
                try:
                    raw = getattr(resp, "raw", None)
                    result = raw if raw else resp
                    yield result
                finally:
                    if raw:
                        conn = raw._connection
                        if conn is not None:
                            conn.close()
                    result.close()