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

Skip to content

Commit af9d10a

Browse files
committed
logging: enhanced HTTPHandler
1 parent 1b5646a commit af9d10a

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

Lib/logging/handlers.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ class HTTPHandler(logging.Handler):
977977
A class which sends records to a Web server, using either GET or
978978
POST semantics.
979979
"""
980-
def __init__(self, host, url, method="GET"):
980+
def __init__(self, host, url, method="GET", secure=False, credentials=None):
981981
"""
982982
Initialize the instance with the host, the request URL, and the method
983983
("GET" or "POST")
@@ -989,12 +989,14 @@ def __init__(self, host, url, method="GET"):
989989
self.host = host
990990
self.url = url
991991
self.method = method
992+
self.secure = secure
993+
self.credentials = credentials
992994

993995
def mapLogRecord(self, record):
994996
"""
995997
Default implementation of mapping the log record into a dict
996998
that is sent as the CGI data. Overwrite in your class.
997-
Contributed by Franz Glasner.
999+
Contributed by Franz Glasner.
9981000
"""
9991001
return record.__dict__
10001002

@@ -1007,7 +1009,10 @@ def emit(self, record):
10071009
try:
10081010
import http.client, urllib.parse
10091011
host = self.host
1010-
h = http.client.HTTP(host)
1012+
if self.secure:
1013+
h = http.client.HTTPSConnection(host)
1014+
else:
1015+
h = http.client.HTTPConnection(host)
10111016
url = self.url
10121017
data = urllib.parse.urlencode(self.mapLogRecord(record))
10131018
if self.method == "GET":
@@ -1027,8 +1032,13 @@ def emit(self, record):
10271032
h.putheader("Content-type",
10281033
"application/x-www-form-urlencoded")
10291034
h.putheader("Content-length", str(len(data)))
1035+
if self.credentials:
1036+
import base64
1037+
s = ('u%s:%s' % self.credentials).encode('utf-8')
1038+
s = 'Basic ' + base64.b64encode(s).strip()
1039+
h.putheader('Authorization', s)
10301040
h.endheaders(data if self.method == "POST" else None)
1031-
h.getreply() #can't do anything with the result
1041+
h.getresponse() #can't do anything with the result
10321042
except (KeyboardInterrupt, SystemExit):
10331043
raise
10341044
except:

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Core and Builtins
4040
Library
4141
-------
4242

43+
- logging: Enhanced HTTPHandler with secure and credentials initializers.
44+
4345
- Issue #767645: Set os.path.supports_unicode_filenames to True on Mac OS X
4446
(macpath module).
4547

0 commit comments

Comments
 (0)