From bbc03312e1f793013eadd9da9996bbd1dcfead8a Mon Sep 17 00:00:00 2001 From: Matt Daubney Date: Mon, 19 Dec 2016 13:41:24 +0000 Subject: [PATCH] Try and allow python3 encode/decodes --- graphitesend/graphitesend.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/graphitesend/graphitesend.py b/graphitesend/graphitesend.py index 4b3d24b..3207e2e 100644 --- a/graphitesend/graphitesend.py +++ b/graphitesend/graphitesend.py @@ -377,12 +377,18 @@ def _handle_send_error(self, error): (self.addr, error) ) + def _ascii_encode(self, message): + if type(message) == bytes: + return message.decode('ascii') + else: + return message.encode('ascii') + def _send(self, message): """ Given a message send it to the graphite server. """ - self.socket.sendall(message.encode("ascii")) + self.socket.sendall(self._ascii_encode(message)) def _send_and_reconnect(self, message): """Send _message_ to Graphite Server and attempt reconnect on failure. @@ -394,12 +400,12 @@ def _send_and_reconnect(self, message): :raises socket.error: When the socket connection is no longer valid. """ try: - self.socket.sendall(message.encode("ascii")) + self.socket.sendall(self._ascii_encode(message)) except (AttributeError, socket.error): if not self.autoreconnect(): raise else: - self.socket.sendall(message.encode("ascii")) + self.socket.sendall(self._ascii_encode(message)) def _presend(self, message): """