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 <j@w1.fi>
This commit is contained in:
Jouni Malinen 2019-02-02 12:49:23 +02:00
parent 04fa9fc7fd
commit 3a833d95b3

View File

@ -52,7 +52,7 @@ class Ctrl:
break break
self.s = socket.socket(af, socktype) self.s = socket.socket(af, socktype)
self.s.settimeout(5) self.s.settimeout(5)
self.s.sendto("GET_COOKIE", sockaddr) self.s.sendto(b"GET_COOKIE", sockaddr)
reply, server = self.s.recvfrom(4096) reply, server = self.s.recvfrom(4096)
self.cookie = reply self.cookie = reply
self.port = port self.port = port
@ -81,13 +81,24 @@ class Ctrl:
self.started = False self.started = False
def request(self, cmd, timeout=10): def request(self, cmd, timeout=10):
if type(cmd) == str:
try:
cmd2 = cmd.encode()
cmd = cmd2
except UnicodeDecodeError as e:
pass
if self.udp: if self.udp:
self.s.sendto(self.cookie + cmd, self.sockaddr) self.s.sendto(self.cookie + cmd, self.sockaddr)
else: else:
self.s.send(cmd) self.s.send(cmd)
[r, w, e] = select.select([self.s], [], [], timeout) [r, w, e] = select.select([self.s], [], [], timeout)
if r: 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") raise Exception("Timeout on waiting response")
def attach(self): def attach(self):
@ -127,5 +138,9 @@ class Ctrl:
return False return False
def recv(self): def recv(self):
res = self.s.recv(4096) res = self.s.recv(4096).decode()
return res try:
r = str(res)
except UnicodeDecodeError as e:
r = res
return r