From b17c7915a8497a3cdf3647d6904c85048e9b9dc4 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Mon, 14 May 2018 21:51:15 -0400 Subject: [PATCH] Continue project migration to Python 3 --- TorCtl/TorCtl.py | 10 ++++++---- TorCtl/TorUtil.py | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/TorCtl/TorCtl.py b/TorCtl/TorCtl.py index d2cb2a9..24c228c 100755 --- a/TorCtl/TorCtl.py +++ b/TorCtl/TorCtl.py @@ -52,6 +52,8 @@ import time import copy +from io import IOBase + from TorUtil import * if sys.version_info < (2, 5): @@ -867,7 +869,7 @@ def _doSend(self, msg): if len(lines) > 2: amsg = "\n".join(lines[:2]) + "\n" self._debugFile.write(str(time.time())+"\t>>> "+amsg) - self._s.write(msg) + self._s.write(msg.encode()) def set_timer(self, in_seconds, type=None): event = (("650", "TORCTL_TIMER", type),) @@ -929,7 +931,7 @@ def authenticate(self, secret=""): elif self._authType == AUTH_TYPE.PASSWORD: self.authenticate_password(secret) else: - authCookie = open(self._cookiePath, "r") + authCookie = open(self._cookiePath, "rb") self.authenticate_cookie(authCookie) authCookie.close() except ErrorReply, exc: @@ -973,10 +975,10 @@ def authenticate_cookie(self, cookie): """ # read contents if provided a file - if type(cookie) == file: cookie = cookie.read() + if isinstance(cookie, IOBase): cookie = cookie.read() # unlike passwords the cookie contents isn't enclosed by quotes - self.sendAndRecv("AUTHENTICATE %s\r\n" % binascii.b2a_hex(cookie)) + self.sendAndRecv("AUTHENTICATE %s\r\n" % binascii.b2a_hex(cookie).decode()) def get_option(self, name): """Get the value of the configuration option named 'name'. To diff --git a/TorCtl/TorUtil.py b/TorCtl/TorUtil.py index 212788c..032aca3 100644 --- a/TorCtl/TorUtil.py +++ b/TorCtl/TorUtil.py @@ -200,7 +200,7 @@ def readline(self): return result while 1: - s = self._s.recv(128) + s = self._s.recv(128).decode('utf-8') if not s: return None # XXX: This really does need an exception # raise ConnectionClosed()