one will be automatically generated and transmitted if not provided in
the headers argument.
+
+.. method:: HTTPConnection.get_proxy_response_headers()
+
+ Returns a dictionary with the headers of the response received from
+ the proxy server to the CONNECT request.
+
+ If the CONNECT request was not sent, the method returns an empty dictionary.
+
+ .. versionadded:: 3.12
+
+
.. method:: HTTPConnection.connect()
Connect to the server specified when the object was created. By default,
break
return headers
-def parse_headers(fp, _class=HTTPMessage):
- """Parses only RFC2822 headers from a file pointer.
+def _parse_header_lines(header_lines, _class=HTTPMessage):
+ """
+ Parses only RFC2822 headers from header lines.
email Parser wants to see strings rather than bytes.
But a TextIOWrapper around self.rfile would buffer too many bytes
to parse.
"""
- headers = _read_headers(fp)
- hstring = b''.join(headers).decode('iso-8859-1')
+ hstring = b''.join(header_lines).decode('iso-8859-1')
return email.parser.Parser(_class=_class).parsestr(hstring)
+def parse_headers(fp, _class=HTTPMessage):
+ """Parses only RFC2822 headers from a file pointer."""
+
+ headers = _read_headers(fp)
+ return _parse_header_lines(headers, _class)
+
class HTTPResponse(io.BufferedIOBase):
self._tunnel_host = None
self._tunnel_port = None
self._tunnel_headers = {}
- self._proxy_response_headers = None
+ self._raw_proxy_headers = None
(self.host, self.port) = self._get_hostport(host, port)
try:
(version, code, message) = response._read_status()
- self._proxy_response_headers = parse_headers(response.fp)
+ self._raw_proxy_headers = _read_headers(response.fp)
if self.debuglevel > 0:
- for hdr, val in self._proxy_response_headers.items():
- print("header:", hdr + ":", val)
+ for header in self._raw_proxy_headers:
+ print('header:', header.decode())
if code != http.HTTPStatus.OK:
self.close()
finally:
response.close()
+ def get_proxy_response_headers(self):
+ """
+ Returns a dictionary with the headers of the response
+ received from the proxy server to the CONNECT request
+ sent to set the tunnel.
+
+ If the CONNECT request was not sent, the method returns
+ an empty dictionary.
+ """
+ return (
+ _parse_header_lines(self._raw_proxy_headers)
+ if self._raw_proxy_headers is not None
+ else {}
+ )
+
def connect(self):
"""Connect to the host and port specified in __init__."""
sys.audit("http.client.connect", self, self.host, self.port)