本文整理汇总了Python中http.client.HTTPResponse方法的典型用法代码示例。如果您正苦于以下问题:Python client.HTTPResponse方法的具体用法?Python client.HTTPResponse怎么用?Python client.HTTPResponse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http.client
的用法示例。
在下文中一共展示了client.HTTPResponse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _detect_http_redirection
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def _detect_http_redirection(http_response: HTTPResponse, server_host_name: str, server_port: int) -> Optional[str]:
"""If the HTTP response contains a redirection to the same server, return the path to the new location.
"""
next_location_path = None
if 300 <= http_response.status < 400:
location_header = _extract_first_header_value(http_response, "Location")
if location_header:
parsed_location = urlsplit(location_header)
is_relative_url = False if parsed_location.hostname else True
if is_relative_url:
# Yes, to a relative URL; follow the redirection
next_location_path = location_header
else:
is_absolute_url_to_same_hostname = parsed_location.hostname == server_host_name
absolute_url_port = 443 if parsed_location.port is None else parsed_location.port
is_absolute_url_to_same_port = absolute_url_port == server_port
if is_absolute_url_to_same_hostname and is_absolute_url_to_same_port:
# Yes, to an absolute URL to the same server; follow the redirection
next_location_path = f"{parsed_location.path}"
if parsed_location.query:
next_location_path += f"?{parsed_location.query}"
return next_location_path
示例2: _parse_expect_ct_header_from_http_response
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def _parse_expect_ct_header_from_http_response(response: HTTPResponse) -> Optional[ExpectCtHeader]:
raw_expect_ct_header = _extract_first_header_value(response, "expect-ct")
if not raw_expect_ct_header:
return None
max_age = None
report_uri = None
enforce = False
for expect_ct_directive in raw_expect_ct_header.split(","):
expect_ct_directive = expect_ct_directive.strip()
if not expect_ct_directive:
continue
if "max-age" in expect_ct_directive:
max_age = int(expect_ct_directive.split("max-age=")[1].strip())
elif "report-uri" in expect_ct_directive:
report_uri = expect_ct_directive.split("report-uri=")[1].strip(' "')
elif "enforce" in expect_ct_directive:
enforce = True
else:
raise ValueError(f"Unexpected value in Expect-CT header: {repr(expect_ct_directive)}")
return ExpectCtHeader(max_age, report_uri, enforce)
示例3: test_status_lines
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def test_status_lines(self):
# Test HTTP status lines
body = "HTTP/1.1 200 Ok\r\n\r\nText"
sock = FakeSocket(body)
resp = client.HTTPResponse(sock)
resp.begin()
self.assertEqual(resp.read(0), b'') # Issue #20007
self.assertFalse(resp.isclosed())
self.assertFalse(resp.closed)
self.assertEqual(resp.read(), b"Text")
self.assertTrue(resp.isclosed())
self.assertFalse(resp.closed)
resp.close()
self.assertTrue(resp.closed)
body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
sock = FakeSocket(body)
resp = client.HTTPResponse(sock)
self.assertRaises(client.BadStatusLine, resp.begin)
示例4: test_partial_readintos
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def test_partial_readintos(self):
# if we have a length, the system knows when to close itself
# same behaviour than when we read the whole thing with read()
body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText"
sock = FakeSocket(body)
resp = client.HTTPResponse(sock)
resp.begin()
b = bytearray(2)
n = resp.readinto(b)
self.assertEqual(n, 2)
self.assertEqual(bytes(b), b'Te')
self.assertFalse(resp.isclosed())
n = resp.readinto(b)
self.assertEqual(n, 2)
self.assertEqual(bytes(b), b'xt')
self.assertTrue(resp.isclosed())
self.assertFalse(resp.closed)
resp.close()
self.assertTrue(resp.closed)
示例5: test_partial_readintos_incomplete_body
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def test_partial_readintos_incomplete_body(self):
# if the server shuts down the connection before the whole
# content-length is delivered, the socket is gracefully closed
body = "HTTP/1.1 200 Ok\r\nContent-Length: 10\r\n\r\nText"
sock = FakeSocket(body)
resp = client.HTTPResponse(sock)
resp.begin()
b = bytearray(2)
n = resp.readinto(b)
self.assertEqual(n, 2)
self.assertEqual(bytes(b), b'Te')
self.assertFalse(resp.isclosed())
n = resp.readinto(b)
self.assertEqual(n, 2)
self.assertEqual(bytes(b), b'xt')
n = resp.readinto(b)
self.assertEqual(n, 0)
self.assertTrue(resp.isclosed())
self.assertFalse(resp.closed)
resp.close()
self.assertTrue(resp.closed)
示例6: test_response_headers
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def test_response_headers(self):
# test response with multiple message headers with the same field name.
text = ('HTTP/1.1 200 OK\r\n'
'Set-Cookie: Customer="WILE_E_COYOTE"; '
'Version="1"; Path="/acme"\r\n'
'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";'
' Path="/acme"\r\n'
'\r\n'
'No body\r\n')
hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"'
', '
'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
s = FakeSocket(text)
r = client.HTTPResponse(s)
r.begin()
cookies = r.getheader("Set-Cookie")
self.assertEqual(cookies, hdr)
示例7: test_chunked_head
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def test_chunked_head(self):
chunked_start = (
'HTTP/1.1 200 OK\r\n'
'Transfer-Encoding: chunked\r\n\r\n'
'a\r\n'
'hello world\r\n'
'1\r\n'
'd\r\n'
)
sock = FakeSocket(chunked_start + last_chunk + chunked_end)
resp = client.HTTPResponse(sock, method="HEAD")
resp.begin()
self.assertEqual(resp.read(), b'')
self.assertEqual(resp.status, 200)
self.assertEqual(resp.reason, 'OK')
self.assertTrue(resp.isclosed())
self.assertFalse(resp.closed)
resp.close()
self.assertTrue(resp.closed)
示例8: test_readinto_chunked_head
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def test_readinto_chunked_head(self):
chunked_start = (
'HTTP/1.1 200 OK\r\n'
'Transfer-Encoding: chunked\r\n\r\n'
'a\r\n'
'hello world\r\n'
'1\r\n'
'd\r\n'
)
sock = FakeSocket(chunked_start + last_chunk + chunked_end)
resp = client.HTTPResponse(sock, method="HEAD")
resp.begin()
b = bytearray(5)
n = resp.readinto(b)
self.assertEqual(n, 0)
self.assertEqual(bytes(b), b'\x00'*5)
self.assertEqual(resp.status, 200)
self.assertEqual(resp.reason, 'OK')
self.assertTrue(resp.isclosed())
self.assertFalse(resp.closed)
resp.close()
self.assertTrue(resp.closed)
示例9: test_chunked_head
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def test_chunked_head(self):
chunked_start = (
'HTTP/1.1 200 OK\r\n'
'Transfer-Encoding: chunked\r\n\r\n'
'a\r\n'
'hello world\r\n'
'1\r\n'
'd\r\n'
)
sock = FakeSocket(chunked_start + '0\r\n')
resp = client.HTTPResponse(sock, method="HEAD")
resp.begin()
self.assertEqual(resp.read(), b'')
self.assertEqual(resp.status, 200)
self.assertEqual(resp.reason, 'OK')
self.assertTrue(resp.isclosed())
self.assertFalse(resp.closed)
resp.close()
self.assertTrue(resp.closed)
示例10: test_readinto_chunked_head
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def test_readinto_chunked_head(self):
chunked_start = (
'HTTP/1.1 200 OK\r\n'
'Transfer-Encoding: chunked\r\n\r\n'
'a\r\n'
'hello world\r\n'
'1\r\n'
'd\r\n'
)
sock = FakeSocket(chunked_start + '0\r\n')
resp = client.HTTPResponse(sock, method="HEAD")
resp.begin()
b = bytearray(5)
n = resp.readinto(b)
self.assertEqual(n, 0)
self.assertEqual(bytes(b), b'\x00'*5)
self.assertEqual(resp.status, 200)
self.assertEqual(resp.reason, 'OK')
self.assertTrue(resp.isclosed())
self.assertFalse(resp.closed)
resp.close()
self.assertTrue(resp.closed)
示例11: tell
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def tell(self):
"""
Obtain the number of bytes pulled over the wire so far. May differ from
the amount of content returned by :meth:``HTTPResponse.read`` if bytes
are encoded on the wire (e.g, compressed).
"""
return self._fp_bytes_read
示例12: from_httplib
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def from_httplib(ResponseCls, r, **response_kw):
"""
Given an :class:`httplib.HTTPResponse` instance ``r``, return a
corresponding :class:`urllib3.response.HTTPResponse` object.
Remaining parameters are passed to the HTTPResponse constructor, along
with ``original_response=r``.
"""
headers = r.msg
if not isinstance(headers, HTTPHeaderDict):
if PY3: # Python 3
headers = HTTPHeaderDict(headers.items())
else: # Python 2
headers = HTTPHeaderDict.from_httplib(headers)
# HTTPResponse objects in Python 3 don't have a .strict attribute
strict = getattr(r, 'strict', 0)
resp = ResponseCls(body=r,
headers=headers,
status=r.status,
version=r.version,
reason=r.reason,
strict=strict,
original_response=r,
**response_kw)
return resp
# Backwards-compatibility methods for httplib.HTTPResponse
示例13: fileno
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def fileno(self):
if self._fp is None:
raise IOError("HTTPResponse has no file to get a fileno from")
elif hasattr(self._fp, "fileno"):
return self._fp.fileno()
else:
raise IOError("The file-like object this HTTPResponse is wrapped "
"around has no file descriptor")
示例14: parse_from_socket
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def parse_from_socket(cls, sock: socket) -> HTTPResponse:
return cls._parse(sock.recv)
示例15: parse_from_ssl_connection
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import HTTPResponse [as 别名]
def parse_from_ssl_connection(cls, ssl_conn: BaseSslClient) -> HTTPResponse:
return cls._parse(ssl_conn.read)