Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43052a1

Browse files
committed
add context parameter to HTTPHandler (closes #22788)
1 parent f200498 commit 43052a1

4 files changed

Lines changed: 24 additions & 21 deletions

File tree

Doc/library/logging.handlers.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -839,17 +839,22 @@ supports sending logging messages to a Web server, using either ``GET`` or
839839
``POST`` semantics.
840840

841841

842-
.. class:: HTTPHandler(host, url, method='GET', secure=False, credentials=None)
842+
.. class:: HTTPHandler(host, url, method='GET', secure=False, credentials=None, context=None)
843843

844844
Returns a new instance of the :class:`HTTPHandler` class. The *host* can be
845-
of the form ``host:port``, should you need to use a specific port number.
846-
If no *method* is specified, ``GET`` is used. If *secure* is true, an HTTPS
847-
connection will be used. If *credentials* is specified, it should be a
848-
2-tuple consisting of userid and password, which will be placed in an HTTP
845+
of the form ``host:port``, should you need to use a specific port number. If
846+
no *method* is specified, ``GET`` is used. If *secure* is true, a HTTPS
847+
connection will be used. The *context* parameter may be set to a
848+
:class:`ssl.SSLContext` instance to configure the SSL settings used for the
849+
HTTPS connection. If *credentials* is specified, it should be a 2-tuple
850+
consisting of userid and password, which will be placed in a HTTP
849851
'Authorization' header using Basic authentication. If you specify
850852
credentials, you should also specify secure=True so that your userid and
851853
password are not passed in cleartext across the wire.
852854

855+
.. versionchanged:: 3.4.3
856+
The *context* parameter was added.
857+
853858
.. method:: mapLogRecord(record)
854859

855860
Provides a dictionary, based on ``record``, which is to be URL-encoded

Lib/logging/handlers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,8 @@ class HTTPHandler(logging.Handler):
10891089
A class which sends records to a Web server, using either GET or
10901090
POST semantics.
10911091
"""
1092-
def __init__(self, host, url, method="GET", secure=False, credentials=None):
1092+
def __init__(self, host, url, method="GET", secure=False, credentials=None,
1093+
context=None):
10931094
"""
10941095
Initialize the instance with the host, the request URL, and the method
10951096
("GET" or "POST")
@@ -1098,11 +1099,15 @@ def __init__(self, host, url, method="GET", secure=False, credentials=None):
10981099
method = method.upper()
10991100
if method not in ["GET", "POST"]:
11001101
raise ValueError("method must be GET or POST")
1102+
if not secure and context is not None:
1103+
raise ValueError("context parameter only makes sense "
1104+
"with secure=True")
11011105
self.host = host
11021106
self.url = url
11031107
self.method = method
11041108
self.secure = secure
11051109
self.credentials = credentials
1110+
self.context = context
11061111

11071112
def mapLogRecord(self, record):
11081113
"""
@@ -1122,7 +1127,7 @@ def emit(self, record):
11221127
import http.client, urllib.parse
11231128
host = self.host
11241129
if self.secure:
1125-
h = http.client.HTTPSConnection(host)
1130+
h = http.client.HTTPSConnection(host, context=self.context)
11261131
else:
11271132
h = http.client.HTTPConnection(host)
11281133
url = self.url

Lib/test/test_logging.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,29 +1667,20 @@ def test_output(self):
16671667
localhost_cert = os.path.join(here, "keycert.pem")
16681668
sslctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
16691669
sslctx.load_cert_chain(localhost_cert)
1670-
# Unfortunately, HTTPHandler doesn't allow us to change the
1671-
# SSLContext used by HTTPSConnection, so we have to
1672-
# monkeypatch. This can be cleaned up if issue 22788 is
1673-
# fixed.
1674-
old = ssl._create_default_https_context
1675-
def restore_handler():
1676-
ssl._create_default_https_context = old
1677-
self.addCleanup(restore_handler)
1678-
def hack_create_ctx():
1679-
ctx = old()
1680-
ctx.load_verify_locations(localhost_cert)
1681-
return ctx
1682-
ssl._create_default_https_context = hack_create_ctx
1670+
1671+
context = ssl.create_default_context(cafile=localhost_cert)
16831672
else:
16841673
sslctx = None
1674+
context = None
16851675
self.server = server = TestHTTPServer(addr, self.handle_request,
16861676
0.01, sslctx=sslctx)
16871677
server.start()
16881678
server.ready.wait()
16891679
host = 'localhost:%d' % server.server_port
16901680
secure_client = secure and sslctx
16911681
self.h_hdlr = logging.handlers.HTTPHandler(host, '/frob',
1692-
secure=secure_client)
1682+
secure=secure_client,
1683+
context=context)
16931684
self.log_data = None
16941685
root_logger.addHandler(self.h_hdlr)
16951686

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Core and Builtins
3636
Library
3737
-------
3838

39+
- Issue #22788: Add *context* parameter to logging.handlers.HTTPHandler.
40+
3941
- Issue #22921: Allow SSLContext to take the *hostname* parameter even if
4042
OpenSSL doesn't support SNI.
4143

0 commit comments

Comments
 (0)