From 775e8f85ad4cecb78d55b293ed406cdd46d86443 Mon Sep 17 00:00:00 2001 From: lorb Date: Mon, 2 Mar 2020 16:06:47 +0100 Subject: [PATCH 1/3] make it easy to extend HTTPHandler with custom connection. solves issue 39826 --- Lib/logging/handlers.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index 047798f6dc1450..4a120e9f1ec48f 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -1173,6 +1173,20 @@ def mapLogRecord(self, record): """ return record.__dict__ + def getConnection(self, host, secure): + """ + get a HTTP[S]Connection. + + Override when a custom connection is required, for example if + there is a proxy. + """ + import http.client + if secure: + connection = http.client.HTTPSConnection(host, context=self.context) + else: + connection = http.client.HTTPConnection(host) + return connection + def emit(self, record): """ Emit a record. @@ -1180,12 +1194,9 @@ def emit(self, record): Send the record to the Web server as a percent-encoded dictionary """ try: - import http.client, urllib.parse + import urllib.parse host = self.host - if self.secure: - h = http.client.HTTPSConnection(host, context=self.context) - else: - h = http.client.HTTPConnection(host) + h = self.getConnection(host, self.secure) url = self.url data = urllib.parse.urlencode(self.mapLogRecord(record)) if self.method == "GET": From 42563659d235649210bfff9b845e7f739126d791 Mon Sep 17 00:00:00 2001 From: l0rb Date: Mon, 2 Mar 2020 16:06:47 +0100 Subject: [PATCH 2/3] make it easy to extend HTTPHandler with custom connection. solves issue 39826 --- Lib/logging/handlers.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index 047798f6dc1450..4a120e9f1ec48f 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -1173,6 +1173,20 @@ def mapLogRecord(self, record): """ return record.__dict__ + def getConnection(self, host, secure): + """ + get a HTTP[S]Connection. + + Override when a custom connection is required, for example if + there is a proxy. + """ + import http.client + if secure: + connection = http.client.HTTPSConnection(host, context=self.context) + else: + connection = http.client.HTTPConnection(host) + return connection + def emit(self, record): """ Emit a record. @@ -1180,12 +1194,9 @@ def emit(self, record): Send the record to the Web server as a percent-encoded dictionary """ try: - import http.client, urllib.parse + import urllib.parse host = self.host - if self.secure: - h = http.client.HTTPSConnection(host, context=self.context) - else: - h = http.client.HTTPConnection(host) + h = self.getConnection(host, self.secure) url = self.url data = urllib.parse.urlencode(self.mapLogRecord(record)) if self.method == "GET": From 94b52e30fa3b6f4544559786e61c358a94ed1545 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 2 Mar 2020 15:15:06 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2020-03-02-15-15-01.bpo-39826.DglHk7.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-03-02-15-15-01.bpo-39826.DglHk7.rst diff --git a/Misc/NEWS.d/next/Library/2020-03-02-15-15-01.bpo-39826.DglHk7.rst b/Misc/NEWS.d/next/Library/2020-03-02-15-15-01.bpo-39826.DglHk7.rst new file mode 100644 index 00000000000000..e425bbe2d51107 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-03-02-15-15-01.bpo-39826.DglHk7.rst @@ -0,0 +1 @@ +Add getConnection method to logging HTTPHandler to enable custom connections. \ No newline at end of file