From 1dd8feaee55c5f530528b59019a2ad3c357f2905 Mon Sep 17 00:00:00 2001 From: Ben Hutchings Date: Thu, 30 Apr 2020 23:46:42 +0100 Subject: MINION: Convert binary data to text as necessary * HTTP responses are bytes but json.load wants a str. Decode as UTF-8 in strict mode, on the assumption that WEBAPP will always produce valid UTF-8. * os.read returns bytes. Decode as UTF-8 in non-strict (replace) mode. --- lorry-controller-minion | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lorry-controller-minion b/lorry-controller-minion index 6e2affd..0412629 100755 --- a/lorry-controller-minion +++ b/lorry-controller-minion @@ -126,7 +126,7 @@ class MINION(cliapp.Application): logging.error(str(e)) return None - obj = json.loads(body) + obj = json.loads(body.decode('utf-8')) if obj.get('job_id', None): return obj return None @@ -203,7 +203,8 @@ class MINION(cliapp.Application): r, w, x = select.select([self.stdout_fd], [], [], wait_for_output) stdout = stderr = '' if r: - stdout = os.read(self.stdout_fd, read_size) + stdout = os.read(self.stdout_fd, read_size) \ + .decode('utf-8', errors='replace') else: # Finished. if exit != 0: @@ -213,7 +214,7 @@ class MINION(cliapp.Application): data = os.read(self.stdout_fd, read_size) if not data: break - stdout_parts.append(data) + stdout_parts.append(data.decode('utf-8', errors='replace')) stdout = ''.join(stdout_parts) stderr = '' os.remove(self.temp_lorry_filename) @@ -251,7 +252,7 @@ class MINION(cliapp.Application): logging.error(str(e)) return - obj = json.loads(body) + obj = json.loads(body.decode('utf-8')) return obj['kill'] def webapp_request(self, method, path, body): @@ -275,7 +276,8 @@ class MINION(cliapp.Application): conn.close() if response.status != http.client.OK: - raise WEBAPPError(response.status, response.reason, response_body) + raise WEBAPPError(response.status, response.reason, + response_body.decode('utf-8', errors='replace')) return response_body -- cgit v1.2.1