本文整理匯總了Python中urllib3.response.HTTPResponse方法的典型用法代碼示例。如果您正苦於以下問題:Python response.HTTPResponse方法的具體用法?Python response.HTTPResponse怎麽用?Python response.HTTPResponse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類urllib3.response
的用法示例。
在下文中一共展示了response.HTTPResponse方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_response
# 需要導入模塊: from urllib3 import response [as 別名]
# 或者: from urllib3.response import HTTPResponse [as 別名]
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, )
示例2: test_run_query_no_encoding
# 需要導入模塊: from urllib3 import response [as 別名]
# 或者: from urllib3.response import HTTPResponse [as 別名]
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)
示例3: test_get_cbt_fail_invalid_raw_socket
# 需要導入模塊: from urllib3 import response [as 別名]
# 或者: from urllib3.response import HTTPResponse [as 別名]
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)
示例4: test_get_cbt_no_peer_cert
# 需要導入模塊: from urllib3 import response [as 別名]
# 或者: from urllib3.response import HTTPResponse [as 別名]
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
示例5: test_get_cbt_with_peer_cert
# 需要導入模塊: from urllib3 import response [as 別名]
# 或者: from urllib3.response import HTTPResponse [as 別名]
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
示例6: response_factory
# 需要導入模塊: from urllib3 import response [as 別名]
# 或者: from urllib3.response import HTTPResponse [as 別名]
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
示例7: save_file
# 需要導入模塊: from urllib3 import response [as 別名]
# 或者: from urllib3.response import HTTPResponse [as 別名]
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)
示例8: open_file
# 需要導入模塊: from urllib3 import response [as 別名]
# 或者: from urllib3.response import HTTPResponse [as 別名]
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()