diff -r eb26255e11f1 Lib/http/client.py --- a/Lib/http/client.py Wed Jan 28 11:06:04 2015 +0200 +++ b/Lib/http/client.py Wed Jan 28 07:22:17 2015 -0800 @@ -776,6 +776,9 @@ if line in (b'\r\n', b'\n', b''): break + if self.debuglevel > 0: + print('header:', line.decode()) + def connect(self): """Connect to the host and port specified in __init__.""" self.sock = self._create_connection( diff -r eb26255e11f1 Lib/test/test_httplib.py --- a/Lib/test/test_httplib.py Wed Jan 28 11:06:04 2015 +0200 +++ b/Lib/test/test_httplib.py Wed Jan 28 07:22:17 2015 -0800 @@ -1320,6 +1320,25 @@ self.assertIn(b'CONNECT destination.com', self.conn.sock.data) self.assertIn(b'Host: destination.com', self.conn.sock.data) + def test_tunnel_debuglog(self): + expected_header = 'X-Dummy: 1' + response_text = 'HTTP/1.0 200 OK\r\n{}\r\n\r\n'.format(expected_header) + + def create_connection(address, timeout=None, source_address=None): + return FakeSocket( + response_text, host=address[0], port=address[1]) + + conn = client.HTTPConnection('proxy.com') + conn._create_connection = create_connection + conn.set_debuglevel(1) + + conn.set_tunnel('destination.com') + + with support.captured_stdout() as output: + conn.request('PUT', '/', '') + + lines = output.getvalue().splitlines() + self.assertIn('header: {}'.format(expected_header), lines) @support.reap_threads