From 3a833d95b320861f9f6fc42e4003c7e2aa802d87 Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Sat, 2 Feb 2019 12:49:23 +0200 Subject: [PATCH] wpaspy: Convert to/from str to bytes as needed for python3 The control interface commands use mostly ASCII or UTF-8 strings, so convert input/output to strings/bytes as needed for the socket operations with python3. Signed-off-by: Jouni Malinen --- wpaspy/wpaspy.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/wpaspy/wpaspy.py b/wpaspy/wpaspy.py index 99b565ad2..89e973b98 100644 --- a/wpaspy/wpaspy.py +++ b/wpaspy/wpaspy.py @@ -52,7 +52,7 @@ class Ctrl: break self.s = socket.socket(af, socktype) self.s.settimeout(5) - self.s.sendto("GET_COOKIE", sockaddr) + self.s.sendto(b"GET_COOKIE", sockaddr) reply, server = self.s.recvfrom(4096) self.cookie = reply self.port = port @@ -81,13 +81,24 @@ class Ctrl: self.started = False def request(self, cmd, timeout=10): + if type(cmd) == str: + try: + cmd2 = cmd.encode() + cmd = cmd2 + except UnicodeDecodeError as e: + pass if self.udp: self.s.sendto(self.cookie + cmd, self.sockaddr) else: self.s.send(cmd) [r, w, e] = select.select([self.s], [], [], timeout) if r: - return self.s.recv(4096) + res = self.s.recv(4096).decode() + try: + r = str(res) + except UnicodeDecodeError as e: + r = res + return r raise Exception("Timeout on waiting response") def attach(self): @@ -127,5 +138,9 @@ class Ctrl: return False def recv(self): - res = self.s.recv(4096) - return res + res = self.s.recv(4096).decode() + try: + r = str(res) + except UnicodeDecodeError as e: + r = res + return r