Skip to content

Commit

Permalink
Merge pull request #45 from RyanLassigBanks/encoding_fix
Browse files Browse the repository at this point in the history
Fix base 64 encoding issues with username
  • Loading branch information
omergertel authored Oct 8, 2017
2 parents 3acb18d + 61e0e4e commit 553dadd
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions pyformance/reporters/influx.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
DEFAULT_INFLUX_PASSWORD = None
DEFAULT_INFLUX_PROTOCOL = "http"


class InfluxReporter(Reporter):

"""
Expand Down Expand Up @@ -51,9 +50,8 @@ def _create_database(self):
q = quote("CREATE DATABASE %s" % self.database)
request = Request(url + "?q=" + q)
if self.username:
auth = base64.encodestring(
'%s:%s' % (self.username, self.password))[:-1]
request.add_header("Authorization", "Basic %s" % auth)
auth = _encode_username(self.username, self.password)
request.add_header("Authorization", "Basic %s" % auth.decode('utf-8'))
try:
response = urlopen(request)
_result = response.read()
Expand Down Expand Up @@ -84,12 +82,16 @@ def report_now(self, registry=None, timestamp=None):
url = "%s://%s:%s%s" % (self.protocol, self.server, self.port, path)
request = Request(url, post_data.encode("utf-8"))
if self.username:
auth = base64.encodestring(
'%s:%s' % (self.username, self.password))[:-1]
request.add_header("Authorization", "Basic %s" % auth)
auth = _encode_username(self.username, self.password)
request.add_header("Authorization", "Basic %s" % auth.decode('utf-8'))
try:
response = urlopen(request)
_result = response.read()
response.read()
except URLError as err:
LOG.warning("Cannot write to %s: %s",
self.server, err.reason)


def _encode_username(username, password):
auth_string = ('%s:%s' % (username, password)).encode()
return base64.b64encode(auth_string)

0 comments on commit 553dadd

Please sign in to comment.